var a = function(){ this.a = 0; console.log(this); }; new a();//a a();//b
运行分别看下结果。求达人解释。
结果:
>>> new a(); Object { a=0} Object { a=0} >>> a(); Window portal.do?state=showPage
运行环境是firebug的console
问题补充
02221021 写道
对于this的scope, 通常有4种情况.
1 new 调用, this就是生成的对象.
2 普通方法调用, this指向window.
3 apply或者call, this指向apply或者call的对象. 比如 (function (){ alert (this.b) }).call({b:1}). 这里的this是 {b:1}这个对象.
4 属性调用, this指向该属性的拥有者. 比如a.b.c = function(){alert (this)}. 这个this是指向b的.
大概是这么回事, 说的不大严谨.
1 new 调用, this就是生成的对象.
2 普通方法调用, this指向window.
3 apply或者call, this指向apply或者call的对象. 比如 (function (){ alert (this.b) }).call({b:1}). 这里的this是 {b:1}这个对象.
4 属性调用, this指向该属性的拥有者. 比如a.b.c = function(){alert (this)}. 这个this是指向b的.
大概是这么回事, 说的不大严谨.
恩,明白了些。你说的第4中正是我例子本来要的目的。谢过。
但是
2.好像是不对的如果
this.a==undefined
结果是true。
还有个趣事,见图,这难道是传说中的...