简体   繁体   English

为什么这个简单的回调函数不起作用?

[英]Why isn't this simple callback function working?

I'm trying to use a callback function for the first time and am having trouble getting it working. 我正在尝试第一次使用回调函数,但无法使其正常工作。 Here's a simplified version of my code: 这是我的代码的简化版本:

var unsavedChanges = 1;

function saveData(callback)
{
    // save data

    if(typeof callback === "function")
    {
        unsavedChanges = 0;
        callback.apply(arguments);
    }
}

function nextStep(val)
{
    if(unsavedChanges == 1)
    {
        saveData(nextStep, val);
        return false;
    }

    console.log(val);
}

nextStep("test");

JSFiddle: http://jsfiddle.net/tXqn3/ JSFiddle: http : //jsfiddle.net/tXqn3/

Inside my real "saveData" function I'm saving data using $.ajax() and I have the callback in the .done() function. 在我真正的“ saveData”函数中,我使用$.ajax()保存数据,并且在.done()函数中有回调。 So, this way, if there's unsaved data on the page then it gets saved and then the original function that was called gets executed. 这样,如果页面上有未保存的数据,则将其保存,然后执行被调用的原始函数。

With this code, the value undefined gets printed out to the console when the code runs. 使用此代码,当代码运行时, undefined的值将输出到控制台。 What am I doing wrong? 我究竟做错了什么?

Your code has two problems. 您的代码有两个问题。

First, look at the definition of apply : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply 首先,查看一下apply的定义: https : //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply

You're missing the first argument thisArg , which is what should be used as this within the function you're applying. 你错过了第一个参数thisArg ,这是应该作为this你申请的功能中。

Second, I don't think you actually want to pass arguments into apply. 其次,我认为您实际上并不希望将arguments传递给apply。 What you want is all but the first argument, since the first argument is callback . 您想要的只是第一个参数,因为第一个参数是callback So you can use Array.prototype.slice.call(arguments).slice(1) to get all the arguments except the first, and pass this into apply . 因此,您可以使用Array.prototype.slice.call(arguments).slice(1)获取除第一个参数外的所有参数,并将其传递给apply

Here's a working fiddle with these changes: http://jsfiddle.net/UkdT7/1/ 这是进行这些更改的有效方法: http : //jsfiddle.net/UkdT7/1/

If you want to use a callback that accepts arguments, the simplest, idiomatic way to do this is to pass a closure which takes care of the argument passing: 如果要使用接受参数的回调,最简单,惯用的方法是传递一个闭包,该闭包负责传递参数:

var unsavedChanges = 1;

function saveData(callback)
{
    // save data

    if(typeof callback === "function")
    {
        unsavedChanges = 0;
        callback(); // no callback.apply
    }
}

function nextStep(val)
{
    if(unsavedChanges == 1)
    {
        saveData(function () { 
            nextStep(val);
        });
        return false;
    }

    console.log(val);
}

nextStep("test");

http://jsfiddle.net/mattball/YxDkg http://jsfiddle.net/mattball/YxDkg

apply function gets the thisArg as first arugment and then array as second arguments so you need to do this apply函数将thisArg作为第一个引数,然后将数组作为第二个参数,因此您需要执行此操作

callback.apply(this,arguments);

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Function/apply https://developer.mozilla.org/zh-CN/docs/JavaScript/Reference/Global_Objects/Function/apply

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

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