Javascript 闭包中变量的复制有哪些规则?

发布于 2024-08-26 04:47:33 字数 833 浏览 2 评论 0 原文

我只是想检查一下我对 Javascript 中变量复制的理解。据我所知,变量是通过引用传递/分配的,除非您明确告诉它们使用 new 运算符创建副本。但当谈到使用闭包时我有点不确定。假设我有以下代码:

var myArray = [1, 5, 10, 15, 20];
var fnlist = [];
for (var i in myArray) {
    var data = myArray[i];
    fnlist.push(function() {
        var x = data;
        console.log(x);
    });
}
fnlist[2](); // returns 20

我认为这是因为 fnlist[2] 仅在调用时查找 data 的值。所以我尝试了另一种方法:

var myArray = [1, 5, 10, 15, 20];
var fnlist = [];
for (var i in myArray) {
    var data = myArray[i];
    fnlist.push(function() {
        var x = data;
        return function() {
            console.log(x);         
        }
    }());
}
fnlist[2](); // returns 10

所以现在它返回“正确”的值。我是否可以说它有效,因为函数在调用时会将所有变量引用解析为其“常量”值?或者有更好的方法来解释吗?

任何有关此引用/复制业务的解释/解释链接也将不胜感激。谢谢!

I'd just like to check my understanding of variable copying in Javascript. From what I gather, variables are passed/assigned by reference unless you explicitly tell them to create a copy with the new operator. But I'm a little uncertain when it comes to using closures. Say I have the following code:

var myArray = [1, 5, 10, 15, 20];
var fnlist = [];
for (var i in myArray) {
    var data = myArray[i];
    fnlist.push(function() {
        var x = data;
        console.log(x);
    });
}
fnlist[2](); // returns 20

I gather that this is because fnlist[2] only looks up the value of data at the point where it is invoked. So I tried an alternative tack:

var myArray = [1, 5, 10, 15, 20];
var fnlist = [];
for (var i in myArray) {
    var data = myArray[i];
    fnlist.push(function() {
        var x = data;
        return function() {
            console.log(x);         
        }
    }());
}
fnlist[2](); // returns 10

So now it returns the 'correct' value. Am I right to say that it works because a function resolves all variable references to their 'constant' values when it is invoked? Or is there a better way to explain it?

Any explanations / links to explanations regarding this referencing / copying business would be appreciated as well. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-02 04:47:33

闭包变量在其作用域结束时(即,当您离开定义闭包的函数时)绑定(“保存”在闭包中):

function make_closure() {
   var x = 10;
   var closure = function() { alert(x) }
   x = 20
   return closure;
}

func = make_closure()
func() // what do you think?

您找到的解决方案是完全正确的 - 您引入了另一个作用域并且强制闭包在该“内部”范围内绑定变量。

请参阅此处了解详细信息和说明。

Closure variables are bound ("saved" in the closure) at the moment when its scope ends, that is, when you leave the function where the closure is defined:

function make_closure() {
   var x = 10;
   var closure = function() { alert(x) }
   x = 20
   return closure;
}

func = make_closure()
func() // what do you think?

The solution you've found is perfectly correct - you introduce yet another scope and force closure to bind variables in that "inner" scope.

See here for details and explanations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文