function allyIlliterate() {
//tuce is *not* visible out here
for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
};
//tuce is *not* visible out here
};
function byE40() {
//nish *is* visible out here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
//nish *is* visible out here
};
现在我的问题是:
let 与 var 相比是否具有内存(/性能)优势?
除了浏览器支持之外,我应该使用 let over var 的原因是什么?
在我的代码工作流程中开始使用 let now 而不是 var 是否安全?
谢谢,
右
Node.js 中的 let 比 var 慢很多。无论如何,版本 v6.3.0。有时这是戏剧性的。如果将 var 替换为 let,下面的代码大约慢三倍:
function collatz() {
var maxsteps = 0;
var maxval = 0;
var x = 1;
var n;
var steps;
while (x < 1000000) {
steps = 0;
n = x;
while (n > 1) {
if (n & 1)
n = 3*n + 1;
else
n = n / 2;
steps += 1;
}
if (steps > maxsteps) {
maxsteps = steps;
maxval = x;
}
x += 1;
}
console.log(maxval + ' - ' + maxsteps + ' steps');
}
collatz();