phpjs
Version:
85 lines (73 loc) • 2.99 kB
Markdown
layout: page
title: "JavaScript get_defined_functions function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/get_defined_functions:413
- /functions/view/get_defined_functions
- /functions/view/413
- /functions/get_defined_functions:413
- /functions/413
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's get_defined_functions
{% codeblock funchand/get_defined_functions.js lang:js https://raw.github.com/kvz/phpjs/master/functions/funchand/get_defined_functions.js raw on github %}
function get_defined_functions () {
// From: http://phpjs.org/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');
// * example 1: found;
// * returns 1: 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;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/funchand/get_defined_functions.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/funchand/get_defined_functions.js)
### Example 1
This code
{% codeblock lang:js example %}
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');
found;
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Other PHP functions in the funchand extension
{% render_partial _includes/custom/funchand.html %}