嗨!我是前端小M~~
作用域
-
for (let i = 0; i < 3; i++) { for (var i = 0; i < 3; i++) { console.log(i); // 0 1 2 console.log(i); //0 1 2 } } console.log(i); // i is not defined console.log(i); //3
console.log(a); //Cannot access ‘a‘ before initialization console.log(a); let a = 5; var a = 5;
let test = 5; function test(){ function test(){ console.log(`heihei`); console.log(`heihei`); } } let test = 5; console.log(test); //5 ====变量提升=====> console.log(test); //5 console.log(typeof test);//num console.log(typeof test);//num test(); //未声明 5();//???
let b = 2; //被a赋值为1 let fn1 = function (){ let a = 1; //局部 b=a; //局部变量赋值给全局 } let fn2 = function (){ console.log(b); //1 } fn1(); fn2();
let fn1 = function () { let a = 1; fn2(a); } let fn2 = function (x) { console.log(x); //1 } fn1();
let fn1 = function () { let a = 1; return a; //1 } let result = fn1();//1 let fn2 = function () { console.log(result);//1 } fn2();