windows环境下与node环境下的this比较: (1)windows
a. var pet ={ word:'....', speak:function(){ console.log(this.word); console.log(this === pet); } } pet.speak(); 结果:... true b. function pet(word){ this.word = word; console.log(this === window); } pet('....'); 结果:true c. function pet(word){ this.word = word; this.speak = function(){ console.log(this); } } var p = new pet('...'); p.speak(); 结果:petspeak: ()word: "..."__proto__: Object(2)node
a. var pet ={ word:'....', speak:function(){ console.log(this.word); console.log(this === pet); } } pet.speak(); 结果:... true b. function pet(word){ this.word = word; console.log(this === window); } pet('....'); 结果:error 修改: function pet(word){ this.word = word; console.log(this === global); } pet('....'); c. function pet(word){ this.word = word; this.speak = function(){ console.log(this); } } var p = new pet('...'); p.speak(); 结果:pet { word: '...', speak: [Function] }