views:

83

answers:

4

There's a function in PHP where you can get a list of the user defined functions that are currently defined, is there something similar in JS?

I'm trying to see a list of functions in Firefox 3, with Firebug enabled.

=/ so far none of the answers work out of the box

+2  A: 

A similar question posted here

Vishal Seth
yeah I saw that but his code snipet didnt work, I got an error =/
codeninja
+3  A: 
var funcs = []; 

for(var prop in window) {
  if(window.hasOwnProperty(prop) && typeof window[prop] === 'function') {
    if(window[prop].toString().indexOf("[native code]") === -1) {
      funcs.push(window[prop]);
    }
  }
}  
Josh Stodola
I get an error when running this in Firefox.. "Operation is not supported" code: "9"
codeninja
@codeninja: try this SSCCE at http://jsbin.com/ufoju3/2/. It works absolutely fine for me in Firefox. @Josh: +1 for the func.
Andy E
+1  A: 

There is no cross browser method. In Internet Explorer, defined variables and functions become members of the window object but are non enumerable. You can check for their existence using funcName in window, but you can't enumerate them using a for...in statement.

Variables that are defined as properties of the window object are enumerable:

function someFunc () {} // is not enumerable
window.someOtherFunc = function () {} // is enumerable

EDIT JScript's implementation is (surprise, surprise) actually wrong, as outlined in this blog post by Eric Lippert.

But I don't think you want to prefix all your variables with window., do you? For a method that will work in some browsers, see Josh Stodola's answer.

Andy E
I dont need a cross browser answer. I only need to do this in firefox. the above methods return an error
codeninja
@codeninja: it looks like Firefox doesn't allow the functions and variables to be enumerable either.
Andy E
@codeninja: Josh's code works fine for me in Firefox - SSCCE at http://jsbin.com/ufoju3/2/
Andy E
+1  A: 

Might be overly simple, but if you have Firebug enabled, log window to the console. Clicking on it will enumerate all of window's members. That will help you if you need a quick, visual list, but if you're acting upon all of window's members, you'll need to use one of the other methods posted in the comments.

Something like for(var i in window) will get you on the right track there.

ajm