变量提升
在预编译阶段先声明变量为undefined,即提升变量的声明。
原理
1.预编译阶段: 变量和函数被放入内存中
2.执行阶段: 代码从上到下依次执行
var a = 1过程:
1.预编译阶段: 声明变量 var a // a = undefined
2.执行阶段: 变量定义 a = 1
//函数声明式
test()
function test() {console.log('1')}
// 1
//函数表达式
console.log(test)
test()
var test = function() {console.log('1')}
//undefined
//Uncaught TypeError: test is not a function
函数声明整体会被提升到当前作用域的顶部,函数表达式也提升到顶部但是只有其变量名提升