变量提升
函数提升
原代码
var num = 10
fun();
function fun() {
console.log(num)
var num = 20
}
预解析
var num //变量提升
//函数提升
function fun() {
var num //变量提升到作用域的最前面
console.log(num)//此时num未赋值
num = 20
}
num = 10
fun();
控制台输出
undefined
原代码
f1();
console.log(b);
console.log(c);
console.log(a);
function f1() {
var a = b = c = 9
console.log(a);
console.log(b);
console.log(c);
}
预解析
function f1() {
//var a = 9
// b = 9
// c = 9
//不是集体声明,b和c是全局变量赋值
var a = b = c = 9
console.log(a); //9
console.log(b); //9
console.log(c); //9
}
f1();
console.log(b); //9
console.log(c); //9
console.log(a); //报错 外部不能调用局部变量