惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
Microsoft Security Blog
Microsoft Security Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
The Hacker News
The Hacker News
L
LINUX DO - 热门话题
T
Tenable Blog
Hugging Face - Blog
Hugging Face - Blog
Google Online Security Blog
Google Online Security Blog
博客园 - Franky
P
Proofpoint News Feed
H
Hacker News: Front Page
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
博客园_首页
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
Forbes - Security
Forbes - Security
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Attack and Defense Labs
Attack and Defense Labs
N
News and Events Feed by Topic
博客园 - 叶小钗
T
Threat Research - Cisco Blogs
aimingoo的专栏
aimingoo的专栏
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hacker News - Newest:
Hacker News - Newest: "LLM"
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
O
OpenAI News
G
Google Developers Blog
Martin Fowler
Martin Fowler
罗磊的独立博客
S
SegmentFault 最新的问题
T
Tor Project blog
量子位

博客园 - JIN Weijie

基于Entity Framework的自定义分页,增删改的通用实现 基于Dapper的分页实现,支持筛选,排序,结果集总数,多表查询,非存储过程 让Windows 7变成WIFI热点 新浪微博RSS生成器Ver 1.0 同步Twitter帐号 修改Thickbox,预加载图片和点击图片前后浏览 ASTreeView 1.5.8发布(ASP.Net树控件) ASTreeView 1.5.3发布(ASP.NET树控件) ASTreeView 1.4.0发布(ASP.NET树控件) ASTreeView 1.3.0发布(ASP.NET树控件) ASTreeView 1.1.1发布(ASP.NET树控件) 在GoDaddy上部署SubText ASTreeView 1.0发布(一个ASP.NET树控件) 自定义增加Windows xp IIS的连接数 vmware增加磁盘空间方法以及出错解决 <=本博客已经转移至jinweijie.com=> [js][asp.net]客户端控制validator [asp.net]优化ViewState [js]remove whitespace for firefox [windows]自动拨号脚本
javascript closure(闭包)的一个示例
JIN Weijie · 2009-01-15 · via 博客园 - JIN Weijie

今天一个同事看到John Resig 的Pro JavaScript Techniques这本书上的37页上有一段关于闭包的javascript代码,怎么调试都运行不正确,于是和他一起研究了一下,代码是这样的:

 1 // Create a new user object that accepts an object of properties  
 2function User( properties ) {  
 3// Iterate through the properties of the object, and make sure  
 4// that it's properly scoped (as discussed previously)  
 5for ( var i in properties ) {   
 6    (function(){  
 7        //using this here is wrong 这里用this是错误的,因为这时this的作用域是匿名函数的  
 8        // Create a new getter for the property  
 9        this"get" + i ] = function() {
10            //这里用properties[i]也是错误的,因为properties[i]作用域是在闭包的外面
11            return properties[i];  
12        }
;  
13        // Create a new setter for the property  
14        this"set" + i ] = function(val) {  
15            properties[i] = val;  
16        }
;  
17    }
)(); }
  
18}
  
19// Create a new user object instance and pass in an object of  
20// properties to seed it with  
21var user = new User({  
22    name: "Bob",  
23    age: 44  
24}
);  
25// Just note that the name property does not exist, as it's private  
26// within the properties object  
27alert( user.name == null );  
28// However, we're able to access its value using the new getname()  
29// method, that was dynamically generated  
30alert( user.getname() == "Bob" );  
31// Finally, we can see that it's possible to set and get the age using  
32// the newly generated functions  
33user.setage( 22 );  
34alert( user.getage() == 22 ); 

这段代码应该是有几处错误的,如红色字体所示,this的作用域是匿名函数的;另一处是properties[i],它的scope是匿名函数外面,所以,代码执行将会不正确。

经过一番调试,应该写成这样:

 1function User( properties ) {  
 2    //这里一定要声明一个变量来指向当前的instance  
 3    var objthis = this;  
 4    for ( var i in properties ) {  
 5        (function(){  
 6                //在闭包内,t每次都是新的,而 properties[i] 的值是for里面的  
 7                var t = properties[i];  
 8                objthis[ "get" + i ] = function() {return t;};  
 9                objthis[ "set" + i ] = function(val) {t = val;};  
10        }
)();   
11    }
  
12}
  
13  
14//测试代码  
15var user = new User({  
16    name: "Bob",  
17    age: 44  
18}
);  
19  
20alert( user.getname());  
21alert( user.getage());  
22  
23user.setname("Mike");  
24alert( user.getname());  
25alert( user.getage());  
26  
27user.setage( 22 );  
28alert( user.getname());  
29alert( user.getage()); 

 这样,代码就是按预想的执行了。

ps: blog搬家,欢迎访问新地址:www.jinweijie.com