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.
JavaScript has a root object, just open a console and type
this, orwindowfor that matter. If you want to list all functions in the global object, just treat the global object as you would any object:You could leave out the
.hasOwnPropertycheck, but remember that all objects, includingwindowcan be traced back to theObject.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.