至关不错的 Javascript 编程风格规范,建议你们采用此规范编写 Javascript。原文连接: http://dojotoolkit.org/developer/StyleGuide 。html
翻译(Translated by):i.feelinglucky{at}gmail.com from http://www.gracecode.com ,转载请注明出处、做者和翻译者,谢谢配合。编程
本文地址: http://code.google.com/p/grace/wiki/DojoStyle 。vim
Any violation to this guide is allowed if it enhances readability.数组
全部的代码都要变成可供他人容易阅读的。less
核心 API 请使用下面的风格:编辑器
结构 | 规则 | 注释 |
模块 | 小写 | 不要使用多重语义(Never multiple words) |
类 | 骆驼 | |
公有方法 | 混合 | 其余的外部调用也可使用 lower_case(),这样的风格 |
公有变量 | 混合 | |
常量 | 骆驼 或 大写 |
下面的虽然不是必要的,但建议使用:ide
结构 | 规则 |
私有方法 | 混合,例子:_mixedCase |
私有变量 | 混合,例子:_mixedCase |
方法(method)参数 | 混合,例子:_mixedCase, mixedCase |
本地(local)变量 | 混合,例子:_mixedCase, mixedCase |
Account,EventHandler
varNodeTypes={ Element:1, DOCUMENT:2}
getInnerHtml(), getXml(),XmlDocument
obj.getSomeValue()
varMyClass=function(){ var _buffer; this.doSomething =function(){ };}
this._somePrivateVariable = statement;
setTopic(topic)// 变量 topic 为 Topic 类型的变量
getHandler();// 避免使用 getEventHandler()
MouseEventHandler,而非MseEvtHdlr。
请再次注意这条规定,这样作得的好处是很是明显的。它能明确的表达表达式所定义的含义。例如:dojo.events.mouse.Handler// 而非 dojo.events.mouse.MouseEventHandler
EventHandlerUIEventHandlerMouseEventHandler
基类能够在明确描述其属性的前提下,缩减其命名:MouseEventHandleras opposed to MouseUIEventHandler.
isNotError, isNotFound 为非法
// vim:ts=4:noet:tw=0:
译注:老外用 VIM 编辑器比较多,此条能够选择遵循。函数
var someExpression =Expression1 +Expression2 +Expression3;
var o = someObject.get( Expression1, Expression2, Expression3);
注:表达式的缩进与变量声明应为一致的。
注:函数的参数应采用明确的缩进,缩进规则与其余块保持一致。
while(!isDone){ doSomething(); isDone = moreToDo();}
if(someCondition){ statements;}elseif(someOtherCondition){ statements;}else{ statements;}
for(initialization; condition; update){ statements;}
while(!isDone){ doSomething(); isDone = moreToDo();}
do{ statements;}while(condition);
switch(condition){case ABC: statements; // fallthroughcase DEF: statements; break;default: statements; break;}
try{ statements;}catch(ex){ statements;}finally{ statements;}
if(condition){ statement;}while(condition){ statement;}for(intialization; condition; update){ statement;}
下面提供了一些基本的函数或者对象的描述方法:布局
function(){ // summary: Soon we will have enough treasure to rule all of New Jersey. // description: Or we could just get a new roomate. // Look, you go find him. He don't yell at you. // All I ever try to do is make him smile and sing around // him and dance around him and he just lays into me. // He told me to get in the freezer 'cause there was a carnival in there. // returns: Look, a Bananarama tape!}
没有返回值描述ui
{ // summary: Dingle, engage the rainbow machine! // description: // Tell you what, I wish I was--oh my g--that beam, // coming up like that, the speed, you might wanna adjust that. // It really did a number on my back, there. I mean, and I don't // wanna say whiplash, just yet, cause that's a little too far, // but, you're insured, right?}
在有的状况下,对于函数的调用和声明是隐义(invisible)的。在这种状况下,咱们没有办法在函数中加入说明等(供程序调用)。若是您遭遇了这种状况,您可使用一个类来封装函数。
注:此此方法只能在函数没有初始化的参数状况下。如过不是,则它们会被忽略。
dojo.declare( "foo", null, { // summary: Phew, this sure is relaxing, Frylock. // description: // Thousands of years ago, before the dawn of // man as we knew him, there was Sir Santa of Claus: an // ape-like creature making crude and pointless toys out // of dino-bones, hurling them at chimp-like creatures with // crinkled hands regardless of how they behaved the // previous year. // returns: Unless Carl pays tribute to the Elfin Elders in space. });
简单的类型的参数能够直接在函数参数定义中注释说明。function(/*String*/ foo,/*int*/ bar)...
下面是几个修饰符供参考:
function(/*String?*/ foo,/*int...*/ bar,/*String[]*/ baz)...
若是你想增长一个描述,你能够将它们移至初始化块。
基本信息格式为: *关键字* 描述字段 ( *key* Descriptive sentence)
参数和变量的格式为: *关键字* ~*类型*~ 描述字段 ( *key* ~*type*~ Descriptive sentence)
注: *关键字* 和 ~*类型*~ 可使用任何字母和数字表述。function(foo, bar){ // foo: String // used for being the first parameter // bar: int // used for being the second parameter}
因为实例变量、原型变量和外部变量的声明是一致的,因此有不少的方法声明、修改变量。具体的如何定义和定位应在变量最早出现的位置指明变量的名称、类型、做用域等信息。
function foo(){ // myString: String // times: int // How many times to print myString // separator: String // What to print out in between myString* this.myString ="placeholder text"; this.times =5;}
foo.prototype.setString =function(myString){ this.myString = myString;}
foo.prototype.toString =function(){ for(int i =0; i <this.times; i++){ dojo.debug(this.myString); dojo.debug(foo.separator); }} foo.separator ="=====";
应使用和对象值和方法一致的标注方式,好比在他们声明的时候:
{ // key: String // A simple value key:"value", // key2: String // Another simple value}
由于函数能够同时返回多个不一样(类型)的值,因此应每一个返回值以后加入返回类型的注释。注释在行内注释便可,若是全部的返回值为同一类型,则指明返回的类型;如为多个不一样的返回值,则标注返回类型为"mixed"。
function(){ if(arguments.length){ return"You passed argument(s)";// String }else{ returnfalse;// Boolean }}
有时候您须要在函数或者类中添加对于此函数和类的功能性流程描述。若是您打算这样作,您可使用 /*======== (= 字符最好出现 5 次或者更多),这样作的好处就是能够不用将这些东西加入代码(译注:原做者的意思可能为代码管理系统)。
这样看起来在 /*===== 和 =====*/ 会有很是长的一段注释,等待功能调整完毕之后就能够考虑是否删除。
/*===== module.pseudo.kwArgs = { // url: String // The location of the file url: "", // mimeType: String // text/html, text/xml, etc mimeType: "" } =====*/
function(/*module.pseudo.kwArgs*/ kwArgs){ dojo.debug(kwArgs.url); dojo.debug(kwArgs.mimeType);}