/* 此模块测试变量作用域 star */ var a ; console.log(a); //undefined function testScope0(){ a = 10; console.log(a); //10 } testScope0(); /* 此模块测试变量作用域 end */ /* 此模块测试变量作用域 star */ var a = 100; console.log(a); //100 function testScope1(){ a = 10; console.log(a); //10 } testScope1(); function testScope2(){ var a = 10; console.log(a); //10 } testScope2(); function testScope3(){ var a = 10; console.log(a); //10 console.log(this.a); //100 } testScope3(); function testScope4(){ a = 10; console.log(a); //10 console.log(this.a); //100 var a =1000; console.log(a);//1000 } testScope4(); /* 此模块测试变量作用域 end */ /* 此模块测试变量作用域 star */ var a = b = null; var c = 1; (function a(v){ console.log(c); //1 console.log(v); //1 return a = v; })(c) console.log(a); //null var a = b = null; var c = 1; function otherScope(c){ console.log(c); //1 a = c; console.log(a); //1 return a; } otherScope(c); console.log(a); //1 /* 此模块测试变量作用域 end */ /* 此模块测试变量作用域 star */ var name = "window"; var obj = { name:"the obj", getName:function(){ return this.name } } console.log(obj.getName()); //the obj /* 此模块测试变量作用域 end */ /* 此模块测试变量作用域 star */ var name = "window"; var obj = { name:"the obj", getName:function(){ return function(){ return this.name; } } } console.log(obj.getName()()); //window /* 此模块测试变量作用域 end */ /* 此模块测试变量作用域 star */ var a; if(a in window){ console.log("yes"); //yes } if(!(a in window)){ var a = 1; } console.log(a); //undefined /* 此模块测试变量作用域 end */