Use PHP functions in JavaScript

JavaScript call_user_func_array

Call a user function which is the first parameter with the arguments contained in array

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
30
function call_user_func_array (cb, parameters) {
    // Call a user function which is the first parameter with the arguments contained in array  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/call_user_func_array    // +   original by: Thiago Mata (http://thiagomata.blog.com)
    // +   revised  by: Jon Hohle
    // +   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') {        if (typeof this[cb] == 'function') {
            func = this[cb];
        } else {
            func = (new Function(null, 'return ' + cb))();
        }    } else if (cb instanceof 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);
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
call_user_func_array('isNaN', ['a']);

Should return

1
true

» Example 2

Running

1
call_user_func_array('isNaN', [1]);

Should return

1
false

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have call_user_func_array goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
30 Dec '08 Permalink

q  @ 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.

Gravatar
Brett Zamir
20 Dec '08 Permalink

q   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 "if (typeof this[func] == 'function') {"
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 "throw new Exception(func + ' is not a valid function');" 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)

1
2
3
4
56
7
8
9
function call_user_func_array (cb, arr) {
    if (typeof cb === 'string') {
        cb = eval(cb);
    }
    else if (cb instanceof Array) {        cb = eval(cb[0]+"['"+cb[1]+"']");
    }
    cb.apply(null, arr);
}


And then, here's a new one, call_user_func():

1
2
3
4
56
7
8
9
function call_user_func (cb) {
    if (typeof cb === 'string') {
        cb = eval(cb);
    }
    else if (cb instanceof Array) {        cb = eval(cb[0]+"['"+cb[1]+"']");
    }
    cb.apply(null, Array.prototype.slice.call(arguments, 1));
}


Works with objects, and regular functions, whether as strings, arrays, or functions.

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ Jon Hohle: Very good work Jon Hohle! Your version will be included!

Gravatar
Jon Hohle
5 Aug '08 Permalink

q   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);
}


Contribute a New function

More functions

In this category

call_user_func
» call_user_func_array
create_function
forward_static_call
forward_static_call_array
func_get_arg
func_get_args
func_num_args
function_exists
get_defined_functions
register_shutdown_function

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !