片段1
var anum = 666; function funcTest1(){ var b = anum * 2; var anum = 6; var c = anum / 2; console.log(b);//NAN,因为anum指向undefined console.log(c);//3 } funcTest1();
片段2
//没进funcTest2 window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; console.log(window.num + myObj.num);//11 + 13 = 24
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; myObj.funcTest2();//进funcTest2,this指向myObj,但没进return。 console.log(window.num + myObj.num);//11 + 13*23 = 310
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; myObj.funcTest2()();//先进funcTest2,this指向myObj,后进return,this指向window。 console.log(window.num + myObj.num);//11*19 + 13*23 = 508
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; myObj.funcTest2();//进funcTest2,this指向myObj,但没进return。 console.log(window.num + myObj.num);//11 + 13*23 = 310 myObj.funcTest2()();//先进funcTest2,this指向myObj,后进return,this指向window。 console.log(window.num + myObj.num);//11*19 + 13*23*23 = 7086
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; var tcal = myObj.funcTest2();//进funcTest2,this指向myObj,但没进return。 console.log(window.num + myObj.num);//11 + 13*23 = 310 tcal();//没进funcTest2,直接进return,this指向window。 console.log(window.num + myObj.num);//11*19 + 13*23 = 508
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; var cal = myObj.funcTest2; var tcal = cal();//进funcTest2,this指向window,但没进return。 console.log(window.num + myObj.num);//11*23 + 13 = 266 tcal();//没进funcTest2,直接进return,this指向window。 console.log(window.num + myObj.num);//11*23*19 + 13 = 4820
window.num = 11; var myObj = { num:13, funcTest2:function(){ this.num *= 23; return function(){ this.num *= 19; } } }; var cal = myObj.funcTest2; var tcal = cal(); myObj.funcTest2();//进两次funcTest2,this第1次指向window,第2次指向myObj,但没进return。 console.log(window.num + myObj.num);//11*23 + 13*23 = 552 tcal();//没进funcTest2,直接进return,this指向window。 console.log(window.num + myObj.num);//11*23*19 + 13*23 = 5106