phpjs
Version:
76 lines (63 loc) • 2.17 kB
Markdown
---
layout: page
title: "JavaScript forward_static_call_array function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/forward_static_call_array:862
- /functions/view/forward_static_call_array
- /functions/view/862
- /functions/forward_static_call_array:862
- /functions/862
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's forward_static_call_array
{% codeblock funchand/forward_static_call_array.js lang:js https://raw.github.com/kvz/phpjs/master/functions/funchand/forward_static_call_array.js raw on github %}
function forward_static_call_array (cb, parameters) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// % note 1: No real relevance to late static binding here; might also use call_user_func_array()
// * example 1: forward_static_call_array('isNaN', ['a']);
// * returns 1: true
// * example 2: forward_static_call_array('isNaN', [1]);
// * returns 2: false
var func;
if (typeof cb === 'string') {
if (typeof this[cb] === 'function') {
func = this[cb];
} else {
func = (new Function(null, 'return ' + cb))();
}
}
else if (Object.prototype.toString.call(cb) === '[object Array]') {
func = eval(cb[0] + "['" + cb[1] + "']");
}
if (typeof func !== 'function') {
throw new Error(func + ' is not a valid function');
}
return func.apply(null, parameters);
}
{% endcodeblock %}
- [view on github](https://github.com/kvz/phpjs/blob/master/functions/funchand/forward_static_call_array.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/funchand/forward_static_call_array.js)
### Example 1
This code
{% codeblock lang:js example %}
forward_static_call_array('isNaN', ['a']);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
forward_static_call_array('isNaN', [1]);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Other PHP functions in the funchand extension
{% render_partial _includes/custom/funchand.html %}