I need to find or list out all the JavaScript methods in a .js
file.
Asked
Active
Viewed 4,055 times
3

Alexander
- 23,432
- 11
- 63
- 73

Sainath437
- 31
- 1
- 2
-
Did you look in the file? What seems to be the problem? – Oded Oct 04 '10 at 07:18
-
List out how? Do you want to write a program to list them out in, say, UI of some application, or do you want to see the list of functions so that you can "see" them and use them for your programming? – Nivas Oct 04 '10 at 07:22
-
possible duplicate of [List of global user defined functions in javascript ?](http://stackoverflow.com/questions/493833/list-of-global-user-defined-functions-in-javascript) – Anurag Uniyal Oct 04 '10 at 07:45
-
same question as http://stackoverflow.com/questions/493833/list-of-global-user-defined-functions-in-javascript? – jebberwocky Oct 04 '10 at 07:27
-
possible duplicate of http://stackoverflow.com/questions/2418388/how-can-get-a-list-of-the-functions-in-a-javascript-file – Anderson Green Jul 02 '12 at 00:35
3 Answers
4
You can programmatically get a list of all the user-defined global functions as follows:
var listOfFunctions = [];
for (var x in window) {
if (window.hasOwnProperty(x) &&
typeof window[x] === 'function' &&
window[x].toString().indexOf('[native code]') > 0) {
listOfFunctions.push(x);
}
}
The listOfFunctions
array will contain the names of all the global functions which are not native.
UPDATE: As @CMS pointed out in the comments below, the above won't work in Internet Explorer 8 and earlier for global function declarations.

Community
- 1
- 1

Daniel Vassallo
- 337,827
- 72
- 505
- 443
-
2You'll get a [surprise](http://blogs.msdn.com/b/ericlippert/archive/2005/05/04/414684.aspx) when you try this on IE<=8. IE can't enumerate identifiers from Function Declarations in the global scope :( – Christian C. Salvadó Oct 04 '10 at 07:32
-
you're welcome. That's IMO one of the worst bugs on IE. Fortunately it's already fixed on IE9. :) – Christian C. Salvadó Oct 04 '10 at 07:41
-
@CMS: Yes I agree that it's an awful bug, but does it have practical importance? ... ie. Is iteration over global function declarations useful in browser scripting (in general)? – Daniel Vassallo Oct 04 '10 at 07:45
0
You would have to write a parser in order to understand all the grammar. You might be able to use an existing parser.

meder omuraliev
- 183,342
- 71
- 393
- 434