ECMAScript 6介绍了该let声明.我听说它被描述为"本地"变量,但我仍然不太确定它与var关键字的行为有何不同.
有什么区别?何时应该let使用var?
以下两个(或两个/两个)代码片段中的哪一个应该在完整的ECMAScript 2015实现中工作:
for (const e of a)
for (const i = 0; i < a.length; i += 1)
根据我的理解,第一个例子应该有效,因为e每次迭代都会初始化.i在第二个版本中是否也应该如此?
我很困惑,因为现有的实现(Babel,IE,Firefox,Chrome,ESLint)似乎并不一致,并且具有const两种循环变体的各种行为的完整实现; 我也无法在标准中找到具体的观点,因此我将非常感激.
此代码记录66次:
(function timer() {
for (var i=0; i<=5; i++) {
setTimeout(function clog() {console.log(i)}, i*1000);
}
})();
Run Code Online (Sandbox Code Playgroud)
但是这段代码......
(function timer() {
for (let i=0; i<=5; i++) {
setTimeout(function clog() {console.log(i)}, i*1000);
}
})();
Run Code Online (Sandbox Code Playgroud)
...记录以下结果:
0
1
2
3
4
5
Run Code Online (Sandbox Code Playgroud)
为什么?
是因为不同地let绑定到内部范围每个项目并var保持最新值i?
ECMAScript 6 let应该提供块范围而不会引起头痛.有些人可以解释为什么i函数中的代码解析为循环中的最后一个值(就像使用var)而不是当前迭代的值?
"use strict";
var things = {};
for (let i = 0; i < 3; i++) {
things["fun" + i] = function() {
console.log(i);
};
}
things["fun0"](); // prints 3
things["fun1"](); // prints 3
things["fun2"](); // prints 3
Run Code Online (Sandbox Code Playgroud)
根据MDNlet在for循环中使用,应该绑定循环体的范围内的变量.当我在块中使用临时变量时,事情就像我期望的那样.为什么这有必要?
"use strict";
var things = {};
for (let i = 0; i < 3; i++) {
let index = i;
things["fun" + i] = function() {
console.log(index);
};
} …Run Code Online (Sandbox Code Playgroud) 我写了一个非常简单的基准:
console.time('var');
for (var i = 0; i < 100000000; i++) {}
console.timeEnd('var')
console.time('let');
for (let i = 0; i < 100000000; i++) {}
console.timeEnd('let')
Run Code Online (Sandbox Code Playgroud)
如果你正在运行Chrome,可以在这里试试(因为NodeJS和Chrome使用相同的JavaScript引擎,虽然版本通常略有不同):
// Since Node runs code in a function wrapper with a different
// `this` than global code, do that:
(function() {
console.time('var');
for (var i = 0; i < 100000000; i++) {}
console.timeEnd('var')
console.time('let');
for (let i = 0; i < 100000000; i++) {}
console.timeEnd('let')
}).call({});Run Code Online (Sandbox Code Playgroud)
结果令我惊讶:
var: 89.162ms
let: 320.473ms
Run Code Online (Sandbox Code Playgroud)
我在Node 4.0.0 && …
考虑到Chrome主要版本尚未发布,Chrome Canary 59 的新型Ignition + Turbofan引擎已经解决了这个问题.测试显示相同的时间let和var声明的循环变量.
原始(现在是静音)问题.
当使用let在forChrome上循环运行速度非常缓慢,相比于移动变量外刚内循环的范围.
for(let i = 0; i < 1e6; i ++);
Run Code Online (Sandbox Code Playgroud)
需要两倍的时间
{ let i; for(i = 0; i < 1e6; i ++);}
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?
Snippet演示了差异,只影响Chrome,只要我记得Chrome支持,就一直如此let.
var times = [0,0]; // hold total times
var count = 0; // number of tests
function test(){
var start = performance.now();
for(let i = 0; i < 1e6; i += 1){};
times[0] += performance.now()-start;
setTimeout(test1,10) …Run Code Online (Sandbox Code Playgroud)What's exactly the action scope of let in a for-loop in JavaScript?
for (let i = 0; i < 3; i++) {
let i = 4;
console.log(i);
}
console.log(i);Run Code Online (Sandbox Code Playgroud)
The external console.log throws an error:
"Uncaught Reference Error: i is not defined"
It proves i is in a block action scope, however, why doesn't the i defined in the for-loop throw any duplicate definition error?
我已经明白为什么这段代码的输出应该是3 3 3.
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1);
}Run Code Online (Sandbox Code Playgroud)
但我无法理解为什么这段代码的输出是0 1 2.
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 1);
}Run Code Online (Sandbox Code Playgroud)
我希望第二个循环的输出更加清晰for。
function first(){
var items = document.getElementsByTagName("li");
for(var x = 0; x < items.length; x++){
items[x].onclick = function() {
console.log(x);
}
}
}
function second(){
var items = document.getElementsByTagName("li");
for(var x = 0; x < items.length; x++){
(function(val) {
items[val].onclick = function() {
console.log(val);
}
})(x);
}
}
function third(){
var items = document.getElementsByTagName("li");
for(let x = 0; x < items.length; x++){
items[x].onclick = function() {
console.log(x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
列表中有4个元素.3个功能的输出:
第一名:4 4 4 4
秒:0 1 2 3
第三名:0 1 …
我正在遍历freecodecamp上的javascript基础,只是为了刷新自己的记忆,当我进入ES6并解释var和let之间的差异时,其中一个示例使我(和我的同事)头疼。
'use strict';
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 2
console.log(i);
// returns "i is not defined"Run Code Online (Sandbox Code Playgroud)
我期望该printNumTwo函数返回undefined,以为到该函数被调用时该变量i不存在。我的一位同事说,将函数表达式分配给变量时,i得到的值为,2因此当您调用函数时,它将始终返回2。
为了验证该理论,我们将原始示例修改为:
'use strict';
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) …Run Code Online (Sandbox Code Playgroud)javascript ×10
ecmascript-6 ×7
let ×4
for-loop ×3
var ×3
scope ×2
closures ×1
const ×1
node.js ×1
performance ×1
settimeout ×1
v8 ×1