var tmp = 123;
if (true) {
tmp = 'abc'; // 報(bào)錯(cuò)
let tmp;
}
一些隱蔽的死區(qū)
function (x = y,y = 1){
..................
}
let x = x;
let在全局作用域定義的變量不會(huì)成為全局屬性
var a = 1;
// 如果在 Node 的 REPL 環(huán)境,可以寫成 global.a
// 或者采用通用方法,寫成 this.a
window.a // 1
let b = 1;
window.b // undefined
塊級(jí)作用域
ES5和ES6的區(qū)別
以下為阮一峰的案例
function f() { console.log('I am outside!'); }
(function () {
if (false) {
// 重復(fù)聲明一次函數(shù)f
function f() { console.log('I am inside!'); }
}
f();
}());
ES5語(yǔ)法的實(shí)際執(zhí)行后的代碼
function f() { console.log('I am outside!'); }
(function () {
function f() { console.log('I am inside!'); }
if (false) {
}
f(); //I am inside!
}());
ES6的實(shí)際執(zhí)行代碼
function f() { console.log('I am outside!'); }
(function () {
var f = undefined
if (false) {
// 重復(fù)聲明一次函數(shù)f
f = function() { console.log('I am inside!'); }
}
f();//報(bào)錯(cuò)
}());