是否需要在循环中声明局部变量?

时间:2015-04-02 15:35:09

标签: ecmascript-6 traceur

我发现以下工作正常:

while ((_next = itr.next()) && !_next.done) {
    ...
}

并且没有事先声明_next,如果我声明变量while ((let _next = itr.next()) ...,traceur实际上会抛出一个意外的关键字错误。

这是ECMAScript 6吗?

1 个答案:

答案 0 :(得分:1)

  

while ((let _next = itr.next()) ...这是ECMAScript 6吗?

没有。 while语句必须包含表达式,而不是变量声明。无论如何,分组运算符内的变量声明都是无效的。自ES5以来,这一点没有改变 使用

var _next;
while ((_next = itr.next()) && !_next.done) {
    …
}

或只是

for (let … of itr) {
    …
}
相关问题