node与windows下的this

xiaoxiao2021-02-28  93

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] }
转载请注明原文地址: https://www.6miu.com/read-46516.html

最新回复(0)