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?

AWF4vk
  • 5,810
  • 3
  • 37
  • 70

2 Answers2

4

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);
    }
}
Dennis
  • 32,200
  • 11
  • 64
  • 79
  • This won't work in a Webkit environment outside of a browser, because 'window' is defined. – AWF4vk Sep 11 '11 at 14:01
  • 1
    @David In global code the `this` keyword references the global object, so you can replace `window` with `this`... – Šime Vidas Sep 11 '11 at 14:05
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);
    }
}