普通代码段 应该 看起来如下:
while (!isDone){
doSomething();
isDone = moreToDo();
}
IF 语句 应该 看起来像这样:
if (someCondition){
statements;
} else if (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;
// fallthrough
case 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; }
空白
操作符 建议 使用空格隔开(包括三元操作符)。
下面的关键字 避免使用 空白隔开:
break
catch
continue
do
else
finally
for
function (如果为匿名函数,例如:var foo = function(){}; )
if
return
switch
this
try
void
while
with
下面的关键字必须使用空白隔开:
case
default
delete
function (如果为申明,例如:function foo(){}; )
in
instanceof
new
throw
typeof
var
逗号(,) 建议 使用空白隔开。
冒号(:) 建议 使用空白隔开。
点(.) 在后部 建议 使用空白隔开。
点(.) 避免 在前部使用空白。
函数调用和方法 避免 使用空白,例如: doSomething(someParameter); // 非 doSomething (someParameter)
逻辑块 之间使用空行。
声明 建议 对齐使其更容易阅读。
注释
文档
下面提供了一些基本的函数或者对象的描述方法:
基本函数信息
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!
}
对象函数信息
没有返回值描述
{
// 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.
}
);
参数
简单类型
简单的类型的参数可以直接在函数参数定义中注释说明。