0

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

4
  • 2
    Could you explain more what you want to achieve? Do you want to make a text file with all the names of the functions or what? Commented Feb 3, 2014 at 18:42
  • possible duplicate of List of global user defined functions in JavaScript? Commented Feb 3, 2014 at 18:45
  • I want to build a web page where the user will be asked to upload a certain javascript file and then all functions found in that file would be captured where the user could select upon which functions tests will be performed. may be in a drop down list or something Commented Feb 3, 2014 at 18:45
  • feel free to steal my getNatives() function from danml.com/packager, i think ittl work for this... Commented Feb 3, 2014 at 18:52

1 Answer 1

1

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.

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

2 Comments

but that says what's in window, not a certain file, and it would hit already-loaded functions, not just the file's.
no but this is not what I am looking for. I would like to have a list of all the functions in the file whether loaded or not. For selection in testing.

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.