Javascript编程规范
原文地址:https://code.google.com/p/j-et/wiki/JavascriptCodingStandard
?
Any violation to this guide is allowed if it enhances readability.
所有的代码都要变成可供他人容易阅读的。
--引用自Dojo Javascript 语法规范
?
结构规则例如类驼峰式ModuleClass()公有方法混合式getPosition()公有变量混合式frameStyle常量大写式DEFAULT_FRAME_LAYOUT?
?
结构规则私有方法混合,例子:mixedCase私有变量混合,例子:mixedCase方法(method)参数混合,例子:mixedCase, mixedCase本地(local)变量混合,例子:mixedCase, mixedCase?
while(!isDone){? ? ? doSomething();? ? ? isDone = moreToDo();}if 语句应该看起来像这样:if(someCondition){? ? ? ? statements;}elseif(someOtherCondition){? ? statements;}else{? ? statements;}for 语句应该看起来像这样:for(initialization; condition; update){? ? ? ? statements;}while 语句应该看起来像这样:while(!isDone){? ? ? ? doSomething();? ? isDone = moreToDo();}do ... while 语句应该看起来像这样:do{? ? ? ? statements;}while(condition);switch 语句应该看起来像这样:switch(condition){case ABC:? ? statements;? ? // ?fallthroughcase DEF:? ? statements;? ? break;default:? ? ? ? statements;? ? break;}try ... catch 语句应该看起来像这样:try{? ? statements;}catch(ex){? ? statements;}finally{? ? statements;}单行的 if - else,while 或者 for 语句也必须加入括号:if(condition){ statement;}while(condition){ statement;}for(intialization; condition; update){ statement;}循环体内的字符串累加使用join方式。 例如:? ? ? ? var r = [];? ? ? ? for (var i=0;i<100;i++){? ? ? ? ? ? ? ? r.push("hello");? ? ? ? }? ? ? ? var k = r.join("");Switch 建议采用hash-tableswitch 可以才用 Object代替 例如:? ? ? ? var a = {? ? ? ? ? ? ? ? "1":doAction1,? ? ? ? ? ? ? ? "2":doAction2,? ? ? ? }? ? ? ? ? ? ? ? function doAction1(){?? ? ? ? }?? ? ? ? function doAction2(){?? ? ? ? }?? ? ? ? a[1]();不建议使用eval不推荐使用eval来执行脚本。除非用来解释json数据。注意 IE 的内存泄露问题