循环中const的ES6内部 [英] ES6 internals of const in for loops

查看:84
本文介绍了循环中const的ES6内部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对在ES6中for循环如何在后台运行感兴趣.

I am interested how for loop operate behind the scenes in ES6.

这是基本示例

var funcs = [];
for (let i = 0; i < 5; i++) {
    funcs.push(function () {
        console.log(i);
    });
};

每个函数将获得正确的i值(从0到4)的原因是因为let创建了5个新作用域,并且函数绑定到了它们对应的作用域.那就是我的想法,这对我来说最有意义.如果真是这样,那么为什么const声明(i)失败是没有意义的,因为它应该创建5个新作用域,并且const变量可以快乐地生活在不同的作用域中. 在让我们使用IIFE来达到相同效果之前,但是该代码所做的是,它基本上为这些函数创建了新作用域,我认为let在幕后做了同样的事情.

The reason each function will get proper i value (from 0 to 4) is because let is creating 5 new scopes and functions are bound to their corresponding scope. Thats what I think and that makes most sense to me. If that is the case then it doesnt make sense why const declaration (i) is failing since it should be creating 5 new scopes and const variables can live happily in different scopes. Before let we have do use IIFE in order to achieve the same effect, but what that code did is that it basically created new scopes for the functions and I thought let is doing the same behind the scenes.

如果以上语句不正确,则必须是let inside for循环仅创建一个作用域,但是我不知道它与var声明有何不同以及函数如何获得正确的i值.为了更加清楚,可以说,let绑定到了一个由for循环创建的新作用域,并且在这种情况下,var声明被提升到了全局作用域,但是那仍然是一个要使用的作用域.

If the above statement is not correct, then it must be that let inside for loop is only creating one scope but then I do not get how is that different from var declaration and how functions get proper i value. To make it even more clear, lets say that let is bound to one new scope which is created by the for loop and var declaration is hoisted to the global scope in this case but thats still one scope to be working with.

任何人都可以在这个话题上分享一些看法吗?

Can anyone share some light on this topic?

推荐答案

每个函数获得正确的i值(从0到4)的原因是因为let创建了5个新作用域,并且函数绑定到了它们对应的作用域.

The reason each function will get proper i value (from 0 to 4) is because let is creating 5 new scopes and functions are bound to their corresponding scope.

是的,这就是发生的情况.有关详细信息(实际上有6个作用域),请参见解释`let`和使用for循环进行块作用域.

Yes, this is what happens. See Explanation of `let` and block scoping with for loops for details (actually there are 6 scopes).

如果是这种情况,那么为什么const声明(i)失败是没有意义的,因为它应该创建5个新作用域,并且const变量可以快乐地生活在不同的作用域中.

If that is the case then it doesnt make sense why const declaration (i) is failing since it should be creating 5 new scopes and const variables can live happily in different scopes.

您在这里的错误假设是constlet相同.是的,在循环体评估中完全有可能创建5个const.但这对于像这样的循环没有意义

Your false assumption here is that const works the same as let. Yes, it would totally be possible for 5 consts to be created in the loop body evaluations. But that just doesn't make sense for a loop like

for (const i=0; i<5; i++) {
    console.log(i);
}

这将导致TypeError: Assignment to constant variable-您不能递增const.因此,您不应在for循环中使用它(请注意,for…infor…of循环又有所不同).

which would lead to a TypeError: Assignment to constant variable - you cannot increment a const. So you shouldn't use it in a for loop (notice that for…in and for…of loops are different again).

好的,可以写类似的东西

OK, one could write something like

let i=0; for (const j=i; i<5; i++) …

,并期望它能够正常工作,并在体内获得5个不同的const j变量.但这也不会发生,因为这是一种完全怪异的写法.如果您希望在每次循环迭代中使用const声明,则最好明确地编写它:

and expect it to work and get 5 different const j variables in the body. But that's not what happens either, as that's a totally weird way to write this. If you want a const declaration in each loop iteration, better write it explicitly:

for (let i=0; i<5; i++) {
    const j=i;
    …
}

这是干净清晰的,实际上可以完成您上面期望的工作.

which is clean and clear and actually does what you might have expected above.

在以下示例中可以显示for循环中的const声明实际上是如何使用的:

How const declarations in for loops are actually meant to be used can be shown in this example:

for (const iterator = {…}; iterator.hasNext(); iterator.step()) {
    … iterator.getValue() …
}

如果我们检查规范,实际上与

If we check the spec, this actually has the same effect as

{
    const iterator = {…};
    for (; iterator.hasNext(); iterator.step()) {
        … iterator.getValue() …
    }
}

表示const ant在循环的开头声明一次.在所有值始终不变之后,在每次迭代中重新声明它都是没有意义的.

which means that the constant is declared once in the head of the loop. It doesn't make sense to re-declare it in every iteration, after all its value is constant anyway.

这篇关于循环中const的ES6内部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆