I need to find or list out all the JavaScript methods in a .js file.
-
Did you look in the file? What seems to be the problem?Oded– Oded2010-10-04 07:18:06 +00:00Commented Oct 4, 2010 at 7: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– Nivas2010-10-04 07:22:20 +00:00Commented Oct 4, 2010 at 7:22
-
same question as stackoverflow.com/questions/493833/…?jebberwocky– jebberwocky2010-10-04 07:27:02 +00:00Commented Oct 4, 2010 at 7:27
-
possible duplicate of List of global user defined functions in javascript ?Anurag Uniyal– Anurag Uniyal2010-10-04 07:45:14 +00:00Commented Oct 4, 2010 at 7:45
-
possible duplicate of stackoverflow.com/questions/2418388/…Anderson Green– Anderson Green2012-07-02 00:35:56 +00:00Commented Jul 2, 2012 at 0:35
Add a comment
|
3 Answers
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.
3 Comments
Christian C. Salvadó
You'll get a surprise when you try this on IE<=8. IE can't enumerate identifiers from Function Declarations in the global scope :(
Christian C. Salvadó
you're welcome. That's IMO one of the worst bugs on IE. Fortunately it's already fixed on IE9. :)
Daniel Vassallo
@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)?