Arch-04-06-DOJO JavaScript 编程规范
相当不错的 Javascript 编程风格规范,建议大家采用此规范编写 Javascript。原文链接:?http://dojotoolkit.org/developer/StyleGuide?。
翻译(Translated by):feelinglucky{at}gmail.com,转载请注明出处、作者和翻译者,谢谢配合。
本文地址:?http://code.google.com/p/grace/wiki/DojoStyle?。
Any violation to this guide is allowed if it enhances readability.
所有的代码都要变成可供他人容易阅读的。
核心 API 请使用下面的风格:
?
结构规则注释模块小写不要使用多重语义(Never multiple words)类骆驼?公有方法混合其他的外部调用也可以使用 lower_case(),这样的风格公有变量混合?常量骆驼 或 大写??
下面的虽然不是必要的,但建议使用:
?
结构规则私有方法混合,例子:_mixedCase私有变量混合,例子:_mixedCase方法(method)参数混合,例子:_mixedCase, mixedCase本地(local)变量混合,例子:_mixedCase, mixedCase?
Account, EventHandler
var NodeTypes = {
? ? Element : 1,
? ? DOCUMENT: 2
}getInnerHtml(), getXml(), XmlDocument
obj.getSomeValue()
var MyClass = 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
EventHandler
UIEventHandler
MouseEventHandler
基类可以在明确描述其属性的前提下,缩减其命名:MouseEventHandler as 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;
} else if (someOtherCondition){
? ? statements;
} else {
? ? statements;
}for (initialization; condition; update){
? ? ? ? statements;
}while (!isDone) {
? ? ? ? doSomething();
? ? isDone = moreToDo();
}do {
? ? ? ? statements;
} while (condition);switch (condition) {
case ABC:
? ? statements;
? ? // ?fallthrough
case 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!
}没有返回值描述
{
? ? // 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 {
? ? ? ? return false; // 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);
}