Javascript Best Practices

Javascript Best Practices from http://www.slideshare.net/cheilmann/javascript-best-practices-1041724

 

无意之中看到这篇文章,想到自己平时写JS时都不注意这些细节,把其中一些有用的记下

 

Avoid globals,Global variables are a terribly bad idea.You run the danger of your code being overwritten by any other javascript added to the page after yours. the workaround is to use closures and the module pattern.

 

Everything is global and can be accessed

Problem:access is not contained,anything  in the page can overwrite what you do.

 

Object Literal: Everything is contained but can be accessed via the object name.

Problem:Repetition of module name leads to huge code and is annoying.

Anonymous Module Nothing is global.

Problem:No access from the outside at all (callbacks,event handlers)

Module Pattern: You need to specify what is global and what isn't-switching syntax in between.

Problem:Repetition of module name,different syntax for inner functions.

Revealing Module Pattern:Keep consistent syntax and mix and match what to make global.

Shortcut notatitions keep your code snappy and easier to read once you got used to it.

is the same as

 

is the same as

 

 

is the same as

 

is the same as

 

By putting these into a configuration object and making this one public we make maintenance very easy adn allow for customization.

 

 

This means that every time the loop runs,JavaScript needs to read the length of the array.You can avoid that by storing the length value in a different variable:

An even shorter way of achieving this is to create a second variable in the preloop condition

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值