- JScript code
var outwrap = function(){ this.heihei = function(){ }; this.heihei.prototype = { b: (this.bb = function(){ var md = this; return function(){ return md; } })() }; }; var o = new outwrap(); alert(o.bb); var h = new o.heihei(); alert(h.b() == window);
h.b() == window结果为true,非常疑惑,为什么h.b()的结果不是o,而是window呢,哪位高人帮忙解惑一下,谢谢
------解决方案--------------------
sorry,不是'直接转',是本域找不到的变量,转到window域去找.
this并不在作用域链里查找,和使用到的变量要在作用域链中查找是不一样的
- JScript code
var fn = function(){ var a = 1; (function(){ alert(a);//1 alert(this) //window })() } fn();
------解决方案--------------------
------解决方案--------------------
关于this的最终回复,参见ECMA-262 10.2.3:
The caller provides the this value. If the this value provided by the caller is not an object (including the case where it is null), then the this value is the global object.
其他的参见Javascript Scope Chain相关资料。
------解决方案--------------------
应该会有人解答你的
走先