又是关于闭包的疑问,比较高级的疑问

ykl4452 2009-11-12 12:33:46
加精

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呢,哪位高人帮忙解惑一下,谢谢
...全文
2570 71 打赏 收藏 转发到动态 举报
写回复
用AI写文章
71 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵国胖胖 2012-10-12
  • 打赏
  • 举报
回复
学习了,我这个菜鸟要到什么时候才能长起来啊
newgoody 2012-09-12
  • 打赏
  • 举报
回复
学习了 真是太深了
Eversnow 2012-08-16
  • 打赏
  • 举报
回复
When a function is not the property of an object, then it is invoked as a function:

var sum = add(3, 4); // sum is 7


When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language. Had the language been designed correctly, when the inner function is invoked, this would still be bound to the this variable of the outer function. A consequence of this error is that a method cannot employ an inner function to help it do its work because the inner function does not share the method's access to the object as its this is bound to the wrong value. Fortunately, there is an easy workaround. If the method defines a variable and assigns it the value of this, the inner function will have access to this through that variable. By convention, the name of that variable is that:

// Augment myObject with a double method.

myObject.double = function ( ) {
var that = this; // Workaround.

var helper = function ( ) {
that.value = add(that.value, that.value)
};

helper( ); // Invoke helper as a function.
};

// Invoke double as a method.

myObject.double( );
document.writeln(myObject.getValue( )); // 6
马尾 2012-02-15
  • 打赏
  • 举报
回复
谢谢LS 的解惑!
真的学习了。
路还很长。继续走!
xz22503c 2009-11-18
  • 打赏
  • 举报
回复
顶 顶 顶 顶 顶 顶 顶
Thinking_In_IT 2009-11-18
  • 打赏
  • 举报
回复
折腾明白 又有什么用?有这时间 让我想想怎么去赚更多的钱~


技术再强,不给您工资 您愿意嘛?





_loehuang_ 2009-11-17
  • 打赏
  • 举报
回复
学习
young1177 2009-11-17
  • 打赏
  • 举报
回复
xwuxin 2009-11-17
  • 打赏
  • 举报
回复
分太少了
cchaha 2009-11-17
  • 打赏
  • 举报
回复
关注
mvpoeiu 2009-11-16
  • 打赏
  • 举报
回复
看不太明白的~~
lsd123 2009-11-16
  • 打赏
  • 举报
回复
.
nwao7890 2009-11-16
  • 打赏
  • 举报
回复
mark
天乐_那由他 2009-11-15
  • 打赏
  • 举报
回复
气氛很好,跑来学习
helloquixote02 2009-11-15
  • 打赏
  • 举报
回复
mark
chenzhaochun 2009-11-15
  • 打赏
  • 举报
回复
纯粹路过~友情一顶~
gavin41 2009-11-15
  • 打赏
  • 举报
回复
关注
sharpidd 2009-11-15
  • 打赏
  • 举报
回复
学习了
tfwin2 2009-11-14
  • 打赏
  • 举报
回复
理解了一点点 thanks 高手们
kuty77 2009-11-14
  • 打赏
  • 举报
回复
都是高手啊
加载更多回复(48)

87,921

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧