node与windows下的this

来源:互联网 发布:苹果软件发布 编辑:程序博客网 时间:2024/06/17 15:08

windows环境下与node环境下的this比较:
(1)windows

a.var pet ={    word:'....',    speak:function(){        console.log(this.word);        console.log(this === pet);    }}pet.speak();结果:...  trueb.function pet(word){    this.word = word;    console.log(this === window);}pet('....');结果:truec.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();结果:...      trueb.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] }
原创粉丝点击