1. // 以下代码执行输出结果是什么 (function d(num) { console.log(num); var num = 10 }(100));//100 (function e(num) { //同名函数覆盖变量 console.log(num); var num = 10; function num() { }; }(100));//num函数 (function f(num) { function num() { }; console.log(num);//num函数 var num = 10 console.log(num);//10 }(100));
2. function m() { console.log(a1); //undefined console.log(a2); //undefined console.log(b1); //undefined console.log(b2); //undefined if (false) { function b1() { }; var a1 = 10; } if (true) { function b2() { }; var a2 = 10; } console.log(a1); //undefined console.log(a2); //10 console.log(b1); //undefined console.log(b2); //b2函数 } m();
3. console.log(a);//a函数 var a = 1; function a() { console.log(2); } console.log(a);//1 a();//报错
//预解析 //var a = 9, a是局部变量 //b = 9 , c = 9 b,c是隐式全局变量 fn3(); console.log(c);//9 console.log(b);//9 console.log(a);//报错 function fn3() { var a = b = c = 9; console.log(a);//9 console.log(b);//9 console.log(c);//9 }
2. var color = "red"; function outer() { var anotherColor = "blue"; function inner() { var tmpColor = color;//tmpColor = ‘red‘ color = anotherColor;//color = ‘blue‘ anotherColor = tmpColor;//anotherColor = ‘red‘ console.log(anotherColor);//‘red‘ } inner(); } outer(); console.log(color);//blue
原文:https://www.cnblogs.com/z-lin/p/10961387.html