Javascript ES6 let 和 var 比较
不同点在于:作用域
var关键词的作用域是最近的函数作用域(如果在函数体的外部就是全局作用域)
let关键词的作用域是最接近的块作用域(如果在任何块外就是全局作用域),这将会比函数作用域更小。
同样, 像var 一样, 使用let 声明的变量也会在其被声明的地方之前可见
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)
};
console.log(tuce);
//tuce is *not* visible out here
}; allyIlliterate(); //undefined
byE40();
function byE40() {
//nish visible here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
console.log(nish); //5
//nish visible here
};
function byE40() {
//nish visible here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
console.log(nish); //5
//nish visible here
};

被折叠的 条评论
为什么被折叠?



