JavaScript get_defined_functions
Returns an array of all defined functions
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 | function get_defined_functions () { // Returns an array of all defined functions // // version: 1008.1718 // discuss at: http://phpjs.org/functions/get_defined_functions // + original by: Brett Zamir (http://brett-zamir.me) // + improved by: Brett Zamir (http://brett-zamir.me) // % note 1: Test case 1: If get_defined_functions can find itself in the defined functions, it worked :) // * example 1: function test_in_array (array, p_val) {for(var i = 0, l = array.length; i < l; i++) {if(array[i] == p_val) return true;} return false;} // * example 1: funcs = get_defined_functions(); // * example 1: found = test_in_array(funcs, 'get_defined_functions'); // * results 1: found == true var i = '', arr = [], already = {}; for (i in this.window) { try { if (typeof this.window[i] === 'function') { if (!already[i]) { already[i] = 1; arr.push(i); } } else if (typeof this.window[i] === 'object') { for (var j in this.window[i]) { if (typeof this.window[j] === 'function' && this.window[j] && !already[j]) { already[j] = 1; arr.push(j); } } } } catch (e) { // Some objects in Firefox throw exceptions when their properties are accessed (e.g., sessionStorage) } } return arr; } |
Examples
Running
1 2 3 | function test_in_array (array, p_val) {for(var i = 0, l = array.length; i < l; i++) {if(array[i] == p_val) return true;} return false;} funcs = get_defined_functions(); found = test_in_array(funcs, 'get_defined_functions'); |
Should result in
1 | found == true |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have get_defined_functions goodness in JavaScript.
And here's get_defined_vars(). As far as iterating deeper over its object properties to get a few other globals, the function turns up 'document' and 'location' which would not otherwise be present.
[CODE="Javascript"]
function get_defined_vars() {
var i = '', ct = 0, obj = {};
for (i in window) {
try {
// if (typeof window[i] !== 'function') { // Uncomment if wish to ignore functions (though in JavaScript, they really are variables) // sessionStorage gives errors here in Mozilla
obj[i] = window[i];
if (typeof window[i] === 'object') {
for (var j in window[i]) { // 'history', 'globalStorage' gives errors here in Mozilla
if (window[j] !== undefined) { // window/parent/top/frames/self give errors here in Mozilla
// if (typeof window[j] !== 'function') { // Uncomment if wish to ignore functions (though in JavaScript, they really are variables)
obj[j] = window[j];
// }
}
}
}
// }
}
catch (e) {
}
ct++;
}
// window.length = ct; // Uncomment if wish to create an array-like object
return obj;
}[/CODE]
Here's a slight improvement...For some reason, in Mozilla at least, you actually have to iterate over the window properties which are objects to get a few other globals not returned by a mere iteration of window itself (in Mozilla, these are: QueryInterface, addEventListener, getComputedStyle).
[CODE="Javascript"]function get_defined_functions() {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir
// % note 1: Test case 1: If get_defined_functions can find itself in the defined functions, it worked :)
// * example 1: function test_in_array(array, p_val) {for(var i = 0, l = array.length; i < l; i++) {if(array[i] == p_val) return true;} return false;}
// * example 1: funcs = get_defined_functions();
// * example 1: found = test_in_array(funcs, 'get_defined_functions');
// * results 1: found == true
var i = '', arr = [], already = {};
for (i in window) {
try {
if (typeof window[i] === 'function') {
if (!already[i]) {
already[i] = 1;
arr.push(i);
}
}
else if (typeof window[i] === 'object') {
for (var j in window[i]) {
if (typeof window[j] === 'function' && window[j] && !already[j]) {
already[j] = 1;
arr.push(j);
}
}
}
}
catch (e) {
}
}
return arr;
}
[/CODE]


Kevin van Zonneveld
30 Dec '08