// 1. 奇怪的现象:
// 变量
var num = 0;
console.log(num); // 0
console.log(num); // undefined
var num = 0;
console.log(num); // undefined
var num = 0;
// 函数
function fn() {
console.log('hello');
}
fn(); // hello
fn(); // hello
function fn() {
console.log('hello');
}
// 2. 预解析过程:
// 1)变量提升,把变量的声明提升到当前作用域的最上面,不包括变量的赋值;
// 2)函数提升,把函数的声明提升到当前作用域的最上面,不包括函数的调用。
console.log(num); // undefined
var num = 0;
// 预解析之后:
var num;
console.log(num);
num = 0;
fn(); // hello
function fn() {
console.log('hello');
}
// 预解析之后:
function fn() {
console.log('hello');
}
fn(); // hello
09-11
300
09-13
279