Dojo Javascript 编程规范 [3]


布局

  块

  普通代码段 应该 看起来如下:

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)

  逻辑块 之间使用空行。

  声明 建议 对齐使其更容易阅读。

布局

  块

  普通代码段 应该 看起来如下:

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)

  逻辑块 之间使用空行。

  声明 建议 对齐使其更容易阅读。


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3