新足迹

 找回密码
 注册

精华好帖回顾

· 80年代经典电视剧译制片之一 - (献给生于70年代的人们) (2008-10-30) zmzhu · 微凉 - 林忆莲2011年初一悉尼演唱会小记 (2011-2-4) patrickzhu
· 征文活动---IT女也糊里糊涂混了10年。(109#-111#赠送点心。) (2009-9-18) chinara · 旅游中遇到的几个人(填坑一) (2008-1-6) smile-
Advertisement
Advertisement
12
返回列表 发新帖
楼主:阿Ka

javascript question [复制链接]

发表于 2010-11-17 11:13 |显示全部楼层
此文章由 o2h2o 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 o2h2o 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 乱码 于 2010-11-17 09:52 发表
本来以为很明白closure在memory的实现,这个例子把我弄糊涂了,找了半天也没找到self execting function和loop缠合起来对closure在内存指针方面的解释。把上面的第二段改了一下,有点小bug。

test1
test2

var div = docu ...

以上的 例子来自 jquery 老大 john resig 的book
Secrets of the JavaScript Ninja

评分

参与人数 1积分 +3 收起 理由
乱码 + 3 感谢分享

查看全部评分

Advertisement
Advertisement

发表于 2010-11-17 11:22 |显示全部楼层
此文章由 乱码 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 乱码 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 o2h2o 于 2010-11-17 11:13 发表

以上的 例子来自 jquery 老大 john resig 的book
Secrets of the JavaScript Ninja


got it, thx!!!

发表于 2010-11-17 11:33 |显示全部楼层
此文章由 乱码 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 乱码 所有!转贴必须注明作者、出处和本声明,并保持内容完整
看了一下,在39页,他没做详细的解释.

在javascript good parts上面function/closure那一章也有这个例子,早晨翻出来看了一下,Douglas也没做深入的解释。

在用chrome debug那个function的时候,能明显的看到closure显示是0/1,但是我想知道对于这种self executing function,传不同的arguments进去,它的closure在内存中是怎么指的,这是我最困扰的地方。

发表于 2010-11-17 11:59 |显示全部楼层
此文章由 o2h2o 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 o2h2o 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 乱码 于 2010-11-17 11:33 发表
看了一下,在39页,他没做详细的解释.

在javascript good parts上面function/closure那一章也有这个例子,早晨翻出来看了一下,Douglas也没做深入的解释。

在用chrome debug那个function的时候,能明显的看到closure显示是0/ ...


Using this executed anonymous function as a wrapper for the for loop (replacing the existing {...}
braces) we can now enforce that the correct value will be enclosed by these handlers. Note that, in order to
achieve this, we pass in the iterator value to the anonymous function and then re-declare it in the arguments.
This means that within the scope of each step of the for loop the i variable is defined a new, giving our
click handler closure the value that it expects
.

在深入解释 至于内存的分布图 估计 研究javascrpit 人不会太在意吧

[ 本帖最后由 o2h2o 于 2010-11-17 12:01 编辑 ]

发表于 2010-11-17 12:14 |显示全部楼层
此文章由 乱码 原创或转贴,不代表本站立场和观点,版权归 oursteps.com.au 和作者 乱码 所有!转贴必须注明作者、出处和本声明,并保持内容完整
原帖由 o2h2o 于 2010-11-17 11:59 发表


Using this executed anonymous function as a wrapper for the for loop (replacing the existing {...}
braces) we can now enforce that the correct value will be enclosed by these handlers. Note that, in ...


closure的实现是有他的[[scope]]的chain和inner function的execution context的scope chain指同一个object,即使outer function不run或者结束了,closure的指针也不realease那块它指的object,从这个角度来说,closure中的value可以maintain,而且让closure很容易理解。

如果不从这个层次上解释,只是泛泛的讲,我相信没有一个人会信服。

发表回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Advertisement
Advertisement
返回顶部