至關不錯的 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);}