JavaScript 中,函数及变量的声明都将被提升到函数的最顶部
重要: 是声明提升, 非赋值提升, 赋值不会提升
实例一:
var math = 100;
// var english 提升了到了这里
console.log("数学=" + math + "英语=" + english);
//数学=100英语=undefined
var english = 99; // english = 99 并不会提升实例二:
var scope = "全局变量";
function t(){
//局部变量声明 (var scope)提升了到了这里,
console.log(scope); //undefined
// scope = "局部变量" 并未提升, 还在这里
var scope = "局部变量";
console.log(scope);//局部变量
}
t();
console.log(scope);//全局变量