js在预编译的时候存在函数提升和变量声明提升,并且函数提升大于变量提升
var a = 1;
function func() {
console.log(a) //
var a = 2
function a() {
console.log(a)
}
}
func()
//根据变量和函数声明存在提升
function func() {
var a;
function a() {
console.log(a)
}
console.log(a) // function a() {console.log(a)}
a = 2
}
var a = 1;
function func() {
console.log(a) //ƒ a() {console.log(a)}
var a = 2
function a() {
console.log(a)
}
console.log(a) //2
}
func()
// 就近原则作用域里面有就去作用域 ,没有的话向上寻找,案例二这里的var a = 1 相当于没有用
function foo() {
var a = 1;
function a() {}
console.log(a);
}
foo()
function foo() {
var a;
function a() {}
a = 1;
console.log(a); //1
}
foo();
function foo() {
console.log(1);
}
foo();
function foo() {
console.log(2);
}
foo();
function foo() {
console.log(1);
}
function foo() {
console.log(2);
}
foo();
foo();
foo();
// 222