javascript 动态添加参数后再执行的方法

function myFunction(test1, test2) {
    //用apply传入的参数必须显式声明,按放入顺序取   
    //alert(extendStr);   
    alert(test1);
    alert(test2);
    //绑定property后不用传入也可以通过函数本身调用,但不能用this   
    alert("myFunction.extendStr = " + myFunction.extendStr);
}


function extendFunction(callbackFunction, extend) {
    var extendStr = "this is extend string!";
    var args = [];
    if (typeof (extend) == "object") {
        for (var property in extend) {
            //绑定property,可以通过函数本身调用   
            callbackFunction[property] = extend[property];
            //把参数值按照顺序放入到数组中,通过apply传入   
            args.push(extend[property]);
        }
    }
    //绑定property,可以通过函数本身调用   
    callbackFunction["extendStr"] = extendStr;
    //把参数值按照顺序放入到数组中,通过apply传入   
    args.push(extendStr);
    //动态调用函数,把参数值数组传入   
    alert('c');
    callbackFunction.apply(this, args);
}
//动态扩展函数并执行,一般用在ajax回调函数的处理中
extendFunction(myFunction, { test1: 'aaa', test2: 'bbb' });

原文地址:https://www.cnblogs.com/amylis_chen/p/1741809.html