提前处理var、 const、 let、 function声明、 class声明 。
var
如果var出现在if语句块中
会提升到所在作用域的头部
console.log(a); // undefined
var a = 1;
如果var出现在with中
不定,不推荐使用with
var a = 1;
function test() {
var o = { a: 2 };
with (o) {
var a = 3;
}
console.log(o.a); // 3
console.log(a); //undefined
}
test();
function声明
会提升到所在作用域的头部并赋值
console.log(a); // function a(){}
function a() {}
如果function声明出现在if语句块中
会声明但不赋值
console.log(a); // undefined
if (true) {
function a() {}
}
const、let、class
声明前都有暂时性死区,声明前不允许使用变量,但不代表没有被预处理
console.log(a) // SyntaxError
let a = 1
console.log(b) // SyntaxError
const b = 2
console.log(C) // SyntaxError
calss C{}
❤️ 转载文章请注明出处,谢谢!❤️