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

  • 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? – Lucio Feb 03 '14 at 18:42
  • possible duplicate of [List of global user defined functions in JavaScript?](http://stackoverflow.com/questions/493833/list-of-global-user-defined-functions-in-javascript) – Patrick Evans Feb 03 '14 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 – user3264801 Feb 03 '14 at 18:45
  • feel free to steal my getNatives() function from http://danml.com/packager/, i think ittl work for this... – dandavis Feb 03 '14 at 18:52

1 Answers1

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.

rafaelcastrocouto
  • 11,781
  • 3
  • 38
  • 63
SK.
  • 4,174
  • 4
  • 30
  • 48
  • 1
    but that says what's in window, not a certain file, and it would hit already-loaded functions, not just the file's. – dandavis Feb 03 '14 at 18:56
  • 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. – user3264801 Feb 03 '14 at 19:25