Javascript call_user_func : Function Handling : PHP functions in JavaScript JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » PHP functions in JavaScript » Function Handling »

 

Javascript call_user_func


Call a user function which is the first parameter
Example 1

Running
1.call_user_func('isNaN', 'a');

Could return
1.true
function call_user_func(cb, parameters) {
    // Call a user function which is the first parameter  
    // 
    // version: 812.3015
    // discuss at: http://phpjs.org/functions/call_user_func
    // +   original by: Brett Zamir
    // *     example 1: call_user_func('isNaN', 'a');
    // *     returns 1: true
    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 Exception(func + ' is not a valid function');
    }

    return func.apply(null, Array.prototype.slice.call(parameters, 1));
}


HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo PHP functions in JavaScript
» Function Handling