UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

30 lines (26 loc) 1.17 kB
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); }