UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

134 lines (106 loc) 4.4 kB
<!-- Generated by Rakefile:build --> <strong> legatee322 </strong> on 2012-08-04 07:10:30 <br /> <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-04-28 18:41:56 <br /> @Diplom@t : Very helpful patch, thank you. I've simplified a bit using the conditional operator and applied against the object when the object is specified as a string (in an array). Feel free to review the changes at http://github.com/kvz/phpjs/raw/master/functions/funchand/call_user_func_array.js and http://github.com/kvz/phpjs/raw/master/functions/funchand/call_user_func.js <hr /> <strong> <a href="difane.com" rel="nofollow">Diplom@t</a> </strong> on 2010-04-28 13:54:02 <br /> Function didn't work for me when i've tried to call it like: var ma = new A(); call_ser_func_array( array(ma, &quot;foo&quot;), [x] ); An error was thrown: &quot;Syntax error: Missing ] after element list&quot; pointing on the line with eval. I have modified the function like one below and it works now: var func; if (typeof cb == 'string') { if (typeof this[cb] == 'function') { func = this[cb]; } else { func = (new Function(null, 'return ' + cb))(); } } else if (cb instanceof Array) { if ( typeof cb[0] == 'string' ) { func = eval(cb[0]+&quot;['&quot;+cb[1]+&quot;']&quot;); } else { func = cb[0][cb[1]]; } } if (typeof func != 'function') { throw new Error(func + ' is not a valid function'); } if ( typeof cb[0] == 'string' ) { return func.apply(null, parameters); } else { return func.apply(cb[0], parameters); } <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-30 10:58:07 <br /> @ Brett Zamir: Yes eval is evil so we'll try to minimize it's use as much as possible, I've made some minor adjustments, let me know if it's ok. <hr /> <strong> <a href="http://bahai-library.com" rel="nofollow">Brett Zamir</a> </strong> on 2008-12-20 11:49:24 <br /> Hi, First, here's a revision. I have a number of differences in mine, some perhaps for better or worse. 1) I can't see any need for the check &amp;quot;if (typeof this[func] == 'function') {&amp;quot; 2) Although 'eval' is not in vogue, I know, the Function constructor is also subject to eval() issues too, so I just used eval(). 3) I didn't see any need for &amp;quot;throw new Exception(func + ' is not a valid function');&amp;quot; in the code posted here, since it would already be a function. 4) I allowed a PHP-style array to be given as the callback to supply a object+method (as in the call_user_func() PHP documentation) <pre><code>function call_user_func_array (cb, arr) { if (typeof cb === 'string') { cb = eval(cb); } else if (cb instanceof Array) { cb = eval(cb[0]+&amp;quot;['&amp;quot;+cb[1]+&amp;quot;']&amp;quot;); } cb.apply(null, arr); }</code></pre> And then, here's a new one, call_user_func(): <pre><code>function call_user_func (cb) { if (typeof cb === 'string') { cb = eval(cb); } else if (cb instanceof Array) { cb = eval(cb[0]+&amp;quot;['&amp;quot;+cb[1]+&amp;quot;']&amp;quot;); } cb.apply(null, Array.prototype.slice.call(arguments, 1)); }</code></pre> Works with objects, and regular functions, whether as strings, arrays, or functions. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-08-27 17:06:10 <br /> @ Jon Hohle: Very good work Jon Hohle! Your version will be included! <hr /> <strong> Jon Hohle </strong> on 2008-08-05 15:32:26 <br /> Here is a cleaner version of call_user_func_array which uses Function:apply. It takes string arg functions or function objects. Because of scoping issues, however, this may behave differently then the method posted above: call_user_func_array = function(func, parameters) { if (typeof func == 'string') { if (typeof this[func] == 'function') { func = this[func]; } else { func = (new Function(null, 'return ' + func))(); } if (typeof func != 'function') { throw new Exception(func + ' is not a valid function'); } } return func.apply(null, parameters); } <hr />