3

I need to find or list out all the JavaScript methods in a .js file.

5

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You'll get a surprise when you try this on IE<=8. IE can't enumerate identifiers from Function Declarations in the global scope :(
you're welcome. That's IMO one of the worst bugs on IE. Fortunately it's already fixed on IE9. :)
@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)?
0

In Notepad++ there is a plugin named Function List - you might find it useful.

Comments

0

You would have to write a parser in order to understand all the grammar. You might be able to use an existing parser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.