1

Is it possible to list / return in an array all javascript functions in my own .js file that begin with the string "_func"?

Done in WebKit's JSCore.

Basically, if my file has a bunch of functions, how do I enumerate those functions?

3
  • 1
    Are those functions global functions? They better not be. :) Commented Sep 11, 2011 at 13:50
  • They can either be global or nested inside other functions... or they can be properties of objects. Commented Sep 11, 2011 at 13:53
  • Hmm. In that case, yes, global, but user-defined. Commented Sep 11, 2011 at 13:54

2 Answers 2

5

You can loop through the members of the window object and test them:

var functions = [];

for( var x in window) {
    if(typeof window[x] === "function" && x.indexOf("_func") === 0) {
        functions.push(x);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This won't work in a Webkit environment outside of a browser, because 'window' is defined.
@David In global code the this keyword references the global object, so you can replace window with this...
2

You can do it by iterating over the members of the window object:

for (var name in window) {
    if (name.match(/^_func/) && typeof window[name] == 'function') {
        console.log(name);
    }
}

1 Comment

This won't work in a Webkit environment outside of a browser, because 'window' is defined.

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.