JS基礎--函式作用域
阿新 • • 發佈:2020-12-13
嗨!我是前端小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();