Skip to content

Dean Edwards

2008/01/08

http://dean.edwards.name/weblog/2007/03/rules/

Rules For JavaScript Library Authors

I wrote this about six months ago before starting work on base2. I decided not to post it at the time as I thought it sounded a little pompous. On reflection, they aren’t bad rules and I managed to stick to them. So, here the rules I wrote for myself back in October.

1. Be unobtrusive

My HTML doesn’t want to know about your JavaScript.

2. Object.prototype is verboten!

This is so important that it needs a rule all to itself. Objects are the basic building blocks of JavaScript functionality. Don’t mess with them.

3. Do Not Over-extend

The less you extend JavaScript’s built-in objects the better. Don’t get me wrong. Native JavaScript objects are a little sparse on useful methods. You will feel obliged to add one or two of your own. But “one or two” is not enough for the creative (library) programmer. Stop! Just add what you need. The less you extend JavaScript’s built-in objects the less you will clash with other libraries.

4. Follow Standards

As a library writer you are defining patterns of JavaScript code. Patterns are signs of weakness in programming languages. Remember, JavaScript and the DOM are continually being specified. If you are going to “fix” something then look to see if it has not already been fixed. Consider available solutions. If you follow standards, then follow them closely (e.g. don’t skip a parameter on a forEach method).

5. Or Follow The Leader

Mozilla leads the way in JavaScript. The creator of the language, Brendan Eich, continues to develop it. New language features are available in Mozilla browsers before any other. If you are going to add language features to JavaScript then look to Mozilla standards first. For example, if you want to extend Array to allow an enumeration method, then call that method forEach instead of each. If you do provide missing language features then follow existing standards closely (see above).

6. Be Flexible

What if I want to modify behaviour without changing the source code of your library? How easy is that? Not easy enough. Make it easier.

7. Manage Memory

People care about memory leaks. Do your job.

8. Eliminate Browser Sniffing

It seems that browser vendors will forever compete by adding new features. ;-) As a library author you must keep up with the latest fashions. It is not good enough to browse Ajaxian occasionally. You must slavishly read every blog to find the next hack. Browser sniffing can be addictive.

9. Small is Better

JavaScript libraries have come of age. Some of them now power premier sites. But we are not all on 2MBit DSL lines. So keep your library small. Better yet, provide a build page that allows me to efficiently build my library according to my needs.

10. The Tenth Rule

Good ol’ tenth rule. You can always rely on the tenth rule. The tenth rule is: be predictable. I should be able to guess what your methods do. And if I don’t know what a method is called then I should be able to guess that too.

11. Bonus Rules

  1. Documentation. Annoying but true.
  2. The more namespacing you use, the less likely I am to remember your phone number.
  3. Remember that potentially millions of people will be executing your code.

For the record, base2 does not alter any native JavaScript objects.

 

适合 JavaScript 库作者的规则

大约六个月之前,我在 base2 的工作启动前写了此文。当时我决定先不发布,因为我认为它听起来有点华而不实。经再三思量,这些规则还不坏,并且我最终保留下来。因此在这里献上我在十月份为自己写的规则。

  1. 不乱入(Be unobtrusive; hlb 的台式翻译XD)
    我的 HTML 不应该去适应你的 JavaScript。
  2. 禁止 Object.prototype
    这条相当重要,所以需要一条完全针对它的规则。对象是构建 JavaScript 功能的基础积木,别胡乱摆弄。
  3. 不要过分扩展
    你对 JavaScript 内建对象的扩展越少越好。别理解错,原生 JavaScript 对象的有用的方法是有点少,你会感到被迫增加一两条你自己的,但是“一两条”对于创造性的(库)程序员来说是不够的。就此停手吧,加你需要的就好。 你对 JavaScript 内建对象的扩展越少,你和其它库的冲突越少。
  4. 跟随标准
    作为一个库的作者,你在定义 JavaScript 代码的模式。在程序设计语言中模式是虚弱的征兆。记住,JavaScript 和文档对象模型在频繁更新,如果你正要“修正”某处,那么注意看看它是否还没被修正。考虑现成的解决方法,如果你跟随标准,就紧随它们(举例来说:别遗漏 forEach 方法的参数)
  5. 或者跟随主导
    Mozilla 主导 JavaScript。这门语言的创造者 Brendan Eich,不断在发展它,新的语言特征在 Mozilla 浏览器中比任何其它浏览器更早可用。如果你要增加语言特征到 JavaScript,那么首先关注 Mozilla 标准。例如:如果你想要扩展 Array 来允许一个枚举方法,那就调用 forEach 方法来代替 each。如果你确实提供缺少的语言特征,那么紧随已存在的标准(见上文)。
  6. 灵活可塑
    如果我要修改行为而不改变你的库的源码呢?那有多容易?不够容易的话,做得更容易些。
  7. 管理内存
    人们担心内存泄露,尽力为之。
  8. 摆脱浏览器嗅探
    貌似浏览器厂商将永远通过添加新的特性来竞争。 ;) 作为一个库的作者,你必须跟得上最入潮的流行。偶尔浏览 Ajaxian 可不够好,你必须奴隶般地读每一个 blog 来寻找下一个 hack。浏览器嗅探会上瘾的
  9. 轻量级的更好
    JavaScript 库已经成熟,其中一些使主流网站得以运作。但是我们不都在用 2MB 的 DSL 线路,所以让你的库保持苗条。然而更好的是,提供一个允许我依我所需有效率地创建库的构造页面。
  10. 第十条规则
    你可以一直依靠第十条规则,这条规则就是:可预料的。我应该可以猜得出你的方法是做什么的,并且如果我不知道一个方法叫做什么,我应该也可以猜得出。
  11. 附赠规则
    1. 文档,让人讨厌但这是真的
    2. 你用了越多的名字空间,我越不可能记住你的电话号码
    3. 记住,可能有数百万的人们会执行你的代码

记上一笔,base2 没有改变任何原生的 JavaScript 对象。

Advertisements
2条评论 leave one →
  1. Unknown permalink
    2008/09/07 02:01

    塑料托盘 仓储笼   塑料托盘 手推车 仓储笼 钢托盘  钢托盘  托盘  塑料托盘 手推车 仓储笼 料箱 钢托盘 托盘  手推车  仓储笼 塑料托盘 料箱  钢托盘 托盘 塑料托盘 手推车 仓储笼 仓储笼 手推车 料箱 塑料托盘 钢托盘 仓储笼 手推车 料箱 塑料托盘 钢托盘 仓储笼 手推车 料箱 塑料托盘 钢托盘 托盘转运车 仓储笼 手推车 料箱 塑料托盘 钢托盘 托盘转运车 手推车 尼龙吊带 尼龙吊带钢丝绳 起重链条 钢板起重钳 钢板起重吊具 吊钩附件 横梁/吊具 成套索具 钢丝绳 起重链条 钢板起重钳 钢板起重吊具 吊钩附件 横梁/吊具 成套索具
     柔性吊带 柔性吊带 柔性吊带 扁平吊带 扁平吊带 扁平吊带 钢板起重吊具 横梁/吊具 吊钩附件 尼龙吊带 电工吊带 钢丝绳 起重链条 工业吊带 柔性吊带
     

  2. Unknown permalink
    2008/09/07 02:01

    平吊带 钢板起重吊具 横梁/吊具 吊钩附件 尼龙吊带 吊带 钢丝绳 起重链条 工业吊带 成套索具 钢板起重钳 托盘  托盘转运车 钢托盘  托盘 塑料托盘 料箱  钢料箱 仓储笼 托盘车  托盘车 托盘车 托盘车  托盘 钢托盘 塑料托盘 料箱 手推车 仓储笼 托盘转运车 托盘车 托盘车 托盘车 托盘车 手推车  手推车 手推车 手推车 仓储笼 钢制料箱 塑料托盘 钢制托盘 登高车 手推车 托盘托盘转运车 威海托盘
    托盘 泰州托盘塑料托盘  手推车 钢托盘 塑料托盘 扬州托盘 各式托盘 泰州托盘  手推车 钢托盘 塑料托盘 扬州托盘 各式托盘 泰州托盘 扬州仓储笼 钢托盘 塑料托盘 泰州托盘 南通托盘 扬州料箱 扬州手推车 仓储笼 钢制料箱  扬州仓储笼  钢托盘 塑料托盘 泰州托盘 南通托盘 扬州料箱 扬州手推车 仓储笼 钢制料箱吊具 江苏吊具  起重链 柔性吊带 酸性吊带

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: