变量提升
变量提升: 在各自的作用域中所声明的变量都会提升到作用域最顶端
提升:【声明提升】,赋值不提升 ---->已经定义为赋值的变量
变量未定义: 返回 xxx is not define
undefined : 表示变量已经定义 在执行当前【使用变量】时,处于【未赋值】状态
例题:
console.log(xsy) // xsy is not defined 变量没有定义
console.log(a) // undefined 变量提升 a变量会提升到最顶部
console.log(b) // b is not defined 变量没有定义 全局变量不能访问局部变量
console.log(c) // c is not defined 变量没有定义 全局变量不能访问局部变量
var a = 'ly'
function fun() {
function c() {
var c = 'ccc'
b = 222
}
c()
console.log(b) // 不打印结果,函数没有执行 函数执行返回:222
var b = 'bbb'
console.log(d) // 不打印结果,函数没有执行 函数执行返回:undefined (因为变量提升,var b;会到提升到最上边,而他的值b可以提升)
}
fun()
var d = 'dddddd'
function fun2(){
return function fun3(){
var e = 'eeeee'
}
}
var res = fun2()
res()
console.log(e) // e is not defined 变量没有定义 因为fun2()没有返回值 全局变量不能访问局部变量