Sainath437
Sainath437

Reputation: 31

How to find JavaScript functions in a JavaScript (.js) file?

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

Upvotes: 3

Views: 4120

Answers (3)

Daniel Vassallo
Daniel Vassallo

Reputation: 344511

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.

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186732

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

Upvotes: 0

Dror
Dror

Reputation: 7303

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

Upvotes: 0

Related Questions