Friday, December 19, 2008

Dynamic Javascript Function Invocation

Such dynamic JavaScript call may become handy in some scenario:
    function functionCall(data, option) {
        if (someCondition) {
            var dynamicFunc = function1;
            var args = [data, option, 1];
        } else {
            var dynamicFunc = function2;
            var args = [data, option, 2];
        }
        var value = dynamicFunc.apply(dynamicFunc, args);
        // processing value 
    }