0

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

3 Answers 3

3
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]);
    }
  }
}  
Sign up to request clarification or add additional context in comments.

2 Comments

I get an error when running this in Firefox.. "Operation is not supported" code: "9"
@codeninja: try this SSCCE at jsbin.com/ufoju3/2. It works absolutely fine for me in Firefox. @Josh: +1 for the func.
1

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.

Comments

1

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.

3 Comments

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

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.