let是ES6中新增关键字。
它的作用类似于var,用来声明变量,但是所声明的变量,只在let命令所在的代码块内有效。
体会下let和var的作用域范围:
function f1() {
var a = 8;
let n = 5;
if (true) {
let n = 10;
var a = 20
}
document.write(n); // 5
document.write(a); // 20
}
f1();
<!DOCTYPE HTML><html><head><scriptsrc="traceur.js"></script><scriptsrc="es6-bootstrap.js"></script><scripttype="text/traceur">var a = [];
for (let i = 0; i < 10; i++) {
a[i] = function() {
document.write(i);
};
}
a[6](); // 6
a[1]();
</script></head><body></body></html>
//61
var -->1010