Closure coding convention suggestion - Ext JS

本文通过解析《EventManager.js》中的具体实例,指出使用闭包保护顶级对象局部变量的做法可能带来的阅读障碍,并建议采用括号包裹函数声明的方式以提高代码可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Hi Jack,

I'm certainly not as productive / good as you are (that's an overstatement, I'm way way less productive than you are) but while reading your code there are certain things I wish you'd have done that will make life much easier for the reader.

In many occassion you'd use a closure to protect the local variables when you define a top level objects

For instance while I was reading EventManager.js

at the beginning I see

Ext.EventManager = function(){
 .............
 600 lines below
}
At first glance I was thinking that this

function(){...}
is the constructor of Ext.EventManager.

Not until I see something like
var pub = {.........}
I start to feel strange and then at at line 321 I see

return pub;
Of course at line 322 we can cleanly see that it is a closure.

}();

So my humble opinion is that if we wrap the function{} literal in parenthesis then it would be very clear from the start that it is not a constructor but a closure.

So instead of

function() {
......
600 lines
......
}();
this would be much easier to read.

(function() {
......
600 lines
......
})();
I hope this makes sense. Maybe 10 years later this will help you pickup ext1.0 again faster :-)
Reply With Quote
  #2  
Old 03-06-2007, 08:15 PM
Default

Considering how I am constantly trying to trim bytes, adding extra ones that serve no purpose doesn't look too appealing. At the top of the file and in the docs it is defined with @singleton (right before the closure starts) which should signal for you that it is a closure (since it can't be a constructor).
Reply With Quote
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值