1

Possible Duplicate:
List of global user defined functions in JavaScript?

I have a java script file (.js) which contains some functions:

function func1(arg)
{
}

function func2(arg)
{
}

...

I want to enumerate all functions defined in js file. Is there a root object in javascript to enum all sub objects by it? (by methods defined in this post)

Ugly solutions:

One approach is to define an array like: var functions = [func1, func2, ...];

Another approach is string processing (such as regular expressions):

/function.+(.+)/

I there better solution for it? thanks.

Community
  • 1
  • 1
mh taqia
  • 3,506
  • 1
  • 24
  • 35

4 Answers4

4

You can try something like:

var Stub = new Object();

Stub.func1 = function(args) {/*Your code*/};
Stub.func2 = function(args) {/*Your code*/};
Stub.func3 = function(args) {/*Your code*/};

// enumerate through functions

for(f in Stub)
{
   if (Stub.hasOwnProperty(f) && typeof Stub[f] === "function")
       console.log(f);
}

I am treating object 'Stub' as a namespace/class here.

EDIT: In case you have no control over the JS file, third party perhaps, you can still use the above technique to figure out the list of functions in the file. I am assuming the js file is defining new functions on the global namespace (window). Here's what you do:

Step 1: Run the above script and store all the functions on window (replace Stub with window) in an array.

Step 2: Dynamically load the new JS file (by adding a script tag linking to the external js file into the HEAD via JavaScript) and after the JS file is loaded (i.e. in ready state), run the above script again.

Step 3: Any function reported in step 2 that was not already visited in step 1 is the new subset introduced by the JS file.

  • So there you have it.
Ashwin Prabhu
  • 9,285
  • 5
  • 49
  • 82
1

You can iterate all global function using window. but it contains many other properties.

for(var fun in window){

   // your code
}
Anoop
  • 23,044
  • 10
  • 62
  • 76
1

This is a pretty hacked up approach; it enumerates the window global object and filters out those that are listed as [native code].

for (var i in window) {
    if (window.hasOwnProperty(i) &&
          'function' === typeof window[i] && 
          !Function.toString.call(window[i]).match('\[native code\]')) { 
        console.log(i) 
    } 
}

Probably not very cross-browser though, it worked on Chrome. It would be better if you could place all your functions inside a namespace.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
1

JavaScript has a root object, just open a console and type this, or window for that matter. If you want to list all functions in the global object, just treat the global object as you would any object:

for (var property in this)
{
    if (this.hasOwnProperty(property) && typeof this[property] === 'function')
    {
        console.log(property + ' is a function');
    }
}

You could leave out the .hasOwnProperty check, but remember that all objects, including window can be traced back to the Object.prototype, so you might end up enumerating prototype-methods, too. What's more, the global object has its own prototype, too: Window.prototype.
A word of warning: when it comes to X-browser issues, the global object is one of the worst objects to deal with. IE, Mozilla, Chrome, Opera and Safari all have differences between them, some more then others.

Elias Van Ootegem
  • 74,482
  • 9
  • 111
  • 149