1 Js基础深入之变量提升

86 阅读1分钟


Js编译器运行阶段,在形成栈内存之后代码执行之前,会首先做一件事,既将 var 变量声明和函数声明中的变量和函数提前

1.var 变量声明:只将声明的变量提前,未赋值

console.log(a) // undefined
var a = 2

// 等价于
var a
console.log(a) // undefined
a = 2

2. 函数声明

fn() // hello world
function fn () {console.log('hello world')} 

3.2. 函数声明优先

先查找变量声明,再查找函数声明

console.log(a) // function a () {console.log('hello world')}
var a = 1
function a () {console.log('hello world')}