UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

86 lines (71 loc) 2.85 kB
--- layout: page title: "JavaScript call_user_func_array function" comments: true sharing: true footer: true alias: - /functions/view/call_user_func_array:364 - /functions/view/call_user_func_array - /functions/view/364 - /functions/call_user_func_array:364 - /functions/364 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's call_user_func_array {% codeblock funchand/call_user_func_array.js lang:js https://raw.github.com/kvz/phpjs/master/functions/funchand/call_user_func_array.js raw on github %} function call_user_func_array (cb, parameters) { // From: http://phpjs.org/functions // + original by: Thiago Mata (http://thiagomata.blog.com) // + revised by: Jon Hohle // + improved by: Brett Zamir (http://brett-zamir.me) // + improved by: Diplom@t (http://difane.com/) // + improved by: Brett Zamir (http://brett-zamir.me) // * example 1: call_user_func_array('isNaN', ['a']); // * returns 1: true // * example 2: call_user_func_array('isNaN', [1]); // * returns 2: false var func; if (typeof cb === 'string') { func = (typeof this[cb] === 'function') ? this[cb] : func = (new Function(null, 'return ' + cb))(); } else if (Object.prototype.toString.call(cb) === '[object Array]') { func = (typeof cb[0] === 'string') ? eval(cb[0] + "['" + cb[1] + "']") : func = cb[0][cb[1]]; } else if (typeof cb === 'function') { func = cb; } if (typeof func !== 'function') { throw new Error(func + ' is not a valid function'); } return (typeof cb[0] === 'string') ? func.apply(eval(cb[0]), parameters) : (typeof cb[0] !== 'object') ? func.apply(null, parameters) : func.apply(cb[0], parameters); } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/funchand/call_user_func_array.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/call_user_func_array.js) ### Example 1 This code {% codeblock lang:js example %} call_user_func_array('isNaN', ['a']); {% endcodeblock %} Should return {% codeblock lang:js returns %} true {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} call_user_func_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 %}