简体   繁体   English

在JavaScript闭包中更改全局变量

[英]Change global variable inside JavaScript closure

I'm trying to change the value of a variable in a closure: 我试图在闭包中更改变量的值:

var myVariable;
$.ajax({
    //stuff....
    success:function(data) {
        myVariable = data;
    }
});

This does not work because myVariable is not visible to the closure. 这不起作用,因为闭包不能看到myVariable How do I change this code so that the value of myVariable changes? 如何更改此代码以使myVariable的值更改?

Contrary to your belief, your code works. 与您的信念相反,您的代码有效。 But seeing what you're trying to do and reading between the lines I'm guessing you're trying to do this: 但看到你正在尝试做什么,并在线之间阅读,我猜你正试图这样做:

var myVariable;
$.ajax({
    //stuff....
    success:function(data) {
        myVariable = data;
    }
});
alert(myVariable); // at this point myVariable is undefined!!

If so, you need to learn about asynchronous functions. 如果是这样,您需要了解异步函数。

Basically, the $.ajax() function returns before actually submitting the ajax request. 基本上, $.ajax()函数在实际提交ajax请求之前返回。 It will do the ajax request later when the browser is not busy executing javascript. 稍后当浏览器不忙于执行javascript时,它会执行ajax请求。 Which means that the assignment will not have happened yet when you try to alert the value of myVariable . 这意味着当您尝试提醒myVariable的值时,分配将不会发生。

Read my response here for more detail: JS global variable not being set on first iteration 在此处阅读我的响应以获取更多详细信息: JS全局变量未在第一次迭代时设置

The only good solution is to change the way you think about coding. 唯一的好办法是改变你对编码的看法。 (There is an arguably bad solution that involves turning the ajax call to synchronous but lets not get in to that, you can google it if you want or read the manual). (有一个可以说是糟糕的解决方案,涉及将ajax调用转为同步,但是如果你想要或者阅读手册,你可以进行谷歌搜索)。 Instead of doing this: 而不是这样做:

var myVariable;
$.ajax({
    //stuff....
    success:function(data) {
        myVariable = data;
    }
});
/*
 * Do lots of stuff with the returned value
 * of the myVariable variable
 *
 */

you now need to write it like this: 你现在需要像这样写:

var myVariable;
$.ajax({
    //stuff....
    success:function(data) {
        myVariable = data;
        /*
         * Do lots of stuff with the returned value
         * of the myVariable variable
         *
         */
    }
});

Basically moving any and all code that you would have written after the ajax call into the success callback. 基本上将在ajax调用之后编写的任何和所有代码移动到成功回调中。 This takes getting used to (judging from how many variants of this question exist on the internet). 这需要习惯(从互联网上存在这个问题的多少变体来判断)。 But once you're used to it it becomes second nature. 但是,一旦你习惯它,它就成了第二天性。

There is a name for this style of programming. 这种编程风格有一个名称。 It is variously known as: "event driven programming" or "continuation-passing style" or "evented programming" . 它被不同地称为: “事件驱动编程”“延续传递风格”“偶数编程” You can google the various terms if you want to know more. 如果您想了解更多信息,可以使用google各种条款。

If that code is in the global scope, myVariable is visible to the inner function. 如果代码是在全球范围内,MYVARIABLE 对内部函数可见。 If you're worried about it being shadowed by a local variable, explicitly access it as a property of the global: 如果您担心它被局部变量遮蔽,请显式访问它作为全局变量的属性:

var myVariable;
$.ajax({
    //stuff....
    success:function(data) {
        window.myVariable = data;
    }
});

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

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