简体   繁体   English

JavaScript回调函数的作用域?

[英]Javascript callback function scope?

I have a javascript function that has a callback then an anonymous function then another callback, and something has gone wrong with the scope. 我有一个JavaScript函数,该函数具有一个回调,然后是一个匿名函数,然后是另一个回调,并且作用域出了点问题。 The parameter callbackFunc is retaining its value from the first function call and not using the new value passed in the 2nd function call. 参数callbackFunc从第一个函数调用中保留其值,而不使用第二个函数调用中传递的新值。

function IsReady(callbackFunc) {   
    if (!IsValid()) return false;
    IsConnected(function () {
        if (typeof (callbackFunc) == 'function')
            callbackFunc();
        return true;
    });    
}

function IsConnected(validCallbackFunc) {
$.post("IsConnected", function (data) {
    if (data.IsValid) {
        if (validCallbackFunc && typeof (validCallbackFunc) == 'function')
            validCallbackFunc();
    }
});
}

$('#SaveButton').click(function () {
    IsReady(SaveInvoice); // works       
});

$('#ExportButton').click(function () {
    // works only if IsConnected() is true     
    // otherwise SaveInvoice is called again
    IsReady(ExportInvoice);  
});

function SaveInvoice() {}
function ExportInvoice() {}

In some circumstances, when I click the ExportButton, the SaveInvoice function is run instead of the ExportInvoice function. 在某些情况下,当我单击ExportButton时,将运行SaveInvoice函数而不是ExportInvoice函数。 I'm guessing that it's a scoping issue - that somehow the old value of callbackFunc has been retained. 我猜这是一个范围问题-某种程度上保留了callbackFunc的旧值。 But I don't quite understand it due to the mix of callback + anonymous function + another callback. 但是由于回调+匿名函数+另一个回调的组合,我不太了解。 I didn't write this code, but I have to fix it. 我没有编写此代码,但是我必须对其进行修复。 Is there anything I can do to clear the value of callbackFunc at the end of IsReady()? 我有什么办法可以清除IsReady()末尾的callbackFunc的值?

IsReady(ExportInvoice) works if IsConnected() is true. 如果IsConnected()为true,则IsReady(ExportInvoice)有效。 If IsConnected() is false then the result is that SaveInvoice() gets executed when in fact nothing should happen (because it is not connected). 如果IsConnected()为false,则结果是将执行SaveInvoice(),而实际上不应该执行任何操作(因为未连接)。

There is no way that the callbackFunc value could be retained between two different calls of the IsReady function. 无法在IsReady函数的两个不同调用之间保留callbackFunc值。

In your code, each time a click event handler is executed, a new scope is created when IsReady is called. 在您的代码中,每次执行click事件处理程序时,在调用IsReady时都会创建一个新的作用域。 Each scope has it's own local parameter callbackFunc . 每个作用域都有自己的局部参数callbackFunc Each scope will define its own anonymous function passed to IsConnected where resides the callbackFunc variable enclosed in a closure. 每个作用域将定义传递给IsConnected自己的匿名函数,该函数驻留在封闭在其中的callbackFunc变量中。

So this is not a scope problem. 因此,这不是范围问题。

To prove it, I emulated your code here: http://jsfiddle.net/pwJC7/ 为了证明这一点,我在这里模拟了您的代码: http : //jsfiddle.net/pwJC7/

In your code you talk about the IsConnected return value. 在您的代码中,您讨论了IsConnected返回值。 This function actually does not return anything. 该函数实际上不返回任何内容。 The connection status seems to be checked through an ajax call returning an XML or JSON data with an IsValid property (emulated by $_post in the fiddle). 连接状态似乎是通过ajax调用来检查的,该调用返回具有IsValid属性的XML或JSON数据(在小提琴中由$_post模拟)。

Maybe your issue is due to this asynchronous call. 也许您的问题是由于此异步调用引起的。 But it's impossible that you experience a call to SaveInvoice function as a consequence of a click to ExportInvoice button with the JavaScript code you provided. 但是,由于使用提供的JavaScript代码单击ExportInvoice按钮,您不可能体验到对SaveInvoice函数的调用。

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

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