Jeekundo.com

以前端技术为主

JS库开发原则

with 12 comments

1.保持无侵入式

html代码不必关心你的javascript在干什么。

2.严禁使用Object.prototype

这一条是如此重要以至于它有资格单独成为一条原则,对象是javascript最基本的构建元素,别把他弄乱了。

3.不要过度扩展

对javascript的内建对象的扩展越少越好。别误会,内建javascript对象本身有用的方法很少,你可能觉得有必要增加自己的一两个方法, 但是一两个对一个有创造力(js库)的程序员来说是不够的,停下来,只增加你真正想要的。越少去扩展内建对象,你与其他框架发生冲突的可能性就越少。

4.紧随标准

作为一个js库开发者,你在为javascript代码建立模式,然而模式在编程语言中意味着差劲,记住,关于javascript和DOM新 标准在不停地修订中,如果你打算去“修正”某些东西,那么先看看那些东西是否已经被修正过了。参考一下已有的解决方案。一旦跟随标准,请别掉队(比如,在 forEach方法中一个参数也不要漏掉)

5.或者跟随领袖

Mozilla引领javascript的潮流。该语言的发明 者,Brendan Eich仍然在改进它。js语言的新特性总是在Mozilla浏览器中最先出现,如果你打算给javascript语言增加新特性,请参考 Mozilla标准先。例如,如果你想增加一个遍历数组的方法,那么请把这个方法命名为forEach而不是each。而如果你是在补全缺失的功能,则要 严格参照现有标准(参见上一条)。

6.灵活一点

如果我想在不动你的js库源代码的情况下更改某个方法的行为,是否足够简单?如果还不够简单就让它更简单一点吧。

7.管理好内存

人们都在乎内存泄漏。把它做好。

8.去除浏览器检测

似乎浏览器厂商在增加新特性的竞争上永无休止。作为js库开发者,你应该紧跟最新的潮流,你不应该仅仅是偶尔看一下Ajaxian,你应该不知疲倦地阅读每一篇blog以便获知最新的hack,浏览器检测会让你越陷越深。

9.越小越好

许多js库已很成熟,其中一些已经被一些很重要的网站所采用。但不是每个人都在用2M的DSL宽带,所以保持你的js库小巧。最好能提供一个build页面让我能够快速的根据我的需要定制出我的js库。

10.第十条

很棒第十条,你总能信赖这一条。第十条是:可预见性。我应该可以根据方法的名字猜到它是干什么的,同样地,如果记不起一个方法的名字,我也应该可以猜得到。

11.附加的几条

1)文档,虽然讨厌但意义重大。

2)多用名字空间,这样我才不至于打电话骚扰你。

3)记住数以百万计的用户潜在用户可能会运行你的代码。

顺便声明,base2没有更改任何内建的javascript对象。

本文翻译自Rules For JavaScript Library Authors,这是Dean Edwards在开发base2时候的一些体会,对于在开发自己的js库的同学应该有较强的借鉴意义。Dean Edwards是公认的javascript高手,jQuery的作者Jhone Resig也很欣赏他。

Related Posts

Written by admin

December 8th, 2009 at 8:19 pm

Posted in 前端技术

12 Responses to 'JS库开发原则'

Subscribe to comments with RSS or TrackBack to 'JS库开发原则'.

  1. This post was very nicely written, and it also contains a lot of useful facts. I appreciated your professional manner of writing this post. Thanks, you have made it easy for me to understand.

  2. Well, I have been reading your blog posts daily and the reason I come on your blog frequently is its compelling content… Regards…

  3. Thank you for such a informative blog. Where else could anyone get that kind of information written in such an incite full way? I have a project that I am just now working on, and I have been looking for such info.

    xmas gift ideas

    22 May 10 at 11:24 am

  4. This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

    xmas gift ideas

    22 May 10 at 12:27 pm

  5. Thanks for taking the time to chat about  this, I feel  fervently  about this and I benefit from learning about this subject.  Please, as you gain information, please add to  this blog with more information.  I have found it really useful.

  6. Thank you for such a fantastic blog. Where else could anyone get that kind of info written in such a perfect way? I have a presentation that I am presently working on, and I have been on the look out for such information.

    Tableware

    2 Jun 10 at 4:01 pm

  7. i know i’m a little off topic, but i just wanted to say i love the layout of your blog. i’m new to the blogegine platform, so any tips on getting my blog looking good would be appreciated.

    tweet tank

    3 Jun 10 at 10:59 pm

  8. 写的不错,以后经常过来看看!一起加油!

  9. It’s really interesting to see how many comments this website is getting. I guess it have a lot of visitors. how do you get that kind of traffic?

    Gale Haggen

    6 Jun 10 at 11:27 pm

  10. 学习了!谢谢分享!

  11. Nice blog! I want you to know I don’t take this stuff for granted. I’m so happy you put this up. Any possiblilty of exchanging links? I really hope so. I’ve bookmarked your page here and will be back. Also visited some of your sponsors, good stuff. I hope you visit my site about Christmas gifts sometime soon and return the favor. Have a fantastic day.

  12. i LOVE This game!

Leave a Reply