简体   繁体   English

为什么不通过function.apply()递归传递函数参数?

[英]Why are function arguments not getting passed recursively via function.apply()?

I'm trying to chain ajax calls to web services upon completion. 我试图在完成时将Ajax调用链接到Web服务。 These are heavy calls that save files from input type=file . 这些是繁重的调用,它们从input type=file保存input type=file

//Here's where I chain the requests (example simplified for readability)
saveData(arg1, 
    saveData, [arg1,
        saveData, [arg1, ....);

//Here's my function definition (example simplified for readability)
function saveData(arg1, callBack, callBackArgs) {
 $.ajax({ //Breakpoint 2. placed here shows all parameters - 
          //arg1, callBack and callBackArgs to be null! What gives?
        type: "POST",
        url: '@Url.Action("MyAction", "MyController")',
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,
        async: true,
        success: function (result) {
            callBack.apply(callBackArgs); //Breakpoint 1. placed here shows callback 
                                          //and callBackArgs to be set perfectly fine
        },
        error: function (error) {
            console.log(error);
        }
    });
}

What am I missing? 我想念什么? Why is function.apply() not working recursively? 为什么function.apply()无法递归工作? Is there any other way to accomplish this? 还有其他方法可以做到这一点吗?

The .apply() function takes two arguments: .apply()函数采用两个参数:

  callback.apply(undefined, callBackArgs);

That assumes that you don't need this to be bound to anything. 假设您不需要this绑定到任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM