views:

1539

answers:

2

Is it possible to get a list of the user defined functions in javascript?

I'm currently using this but it returns functions which aren't user defined:

var functionNames = [];

for (var f in window) if (window.hasOwnProperty(f) && typeof window[f] === 'function')
{
    functionNames.push(f);
}
+2  A: 

I'm assuming you want to filter out native functions. In Firefox, Function.toString() returns the function body, which for native functions, will be in the form:

function addEventListener() { 
    [native code] 
}

You could match the pattern /\[native code\]/ in your loop and omit the functions that match.

Chetan Sastry
Yes, that's what I'm looking for. thanks
Annan
A: 

Using IE: var objs = []; var thing = { makeGreeting: function(text) { return 'Hello ' + text + '!'; } }

for (var obj in window){window.hasOwnProperty(obj) && typeof window[obj] === 'function')objs.push(obj)};

Fails to report 'thing'