user3264801
user3264801

Reputation: 11

Capture functions in Javascript

Is there any specific way for me to capture functions that have been defined in a Javascript file? It is for testing purposes for me to choose functions I want to perform tests in the Javascript file.

Thanking you in advance

Upvotes: 0

Views: 753

Answers (1)

SK.
SK.

Reputation: 4358

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);      
}

Demo

The listOfFunctions array will contain the names of all the global functions which are not native.

The above won't work in Internet Explorer 8 and earlier for global function declarations.

Upvotes: 1

Related Questions