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

时间:2019-02-05 14:46:58

标签: javascript

我正在尝试在完成时将Ajax调用链接到Web服务。这些是繁重的通话,可保存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);
        }
    });
}

我想念什么?为什么function.apply()无法递归工作?还有其他方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:1)

.apply()函数采用两个参数:

  callback.apply(undefined, callBackArgs);

假定您不需要this绑定任何东西。