注:感谢Douglas Crockford对javascript做出的贡献,本文主要翻译自他的文章,原文链接:
http://javascript.crockford.com/code.html
本文主要是列出编写JavaScript代码的好的规范--Code Conventions.
-- 这些都不是JavaScript强制要求的.但是一种好的编码风格会让读你写的代码的人感到很舒服.
1.JavaScript File
JavaScript代码尽量单独保存在一个文件里,文件以.js命名.JavaScript代码尽量不要写在HTML里,写在HTML里会增加HTML页面的大小,而且这些JS不能被缓存和压缩.为了不影响HTML的加载时间可以把引用javascript的<script></script>放在HTML的最后,在</body>后.
2.缩进(Indentation)
缩进是4个字符.一般用tab键也行.不过tab键4个字符并不是被标准的,可能在不同的地方,不同的平台里会有不同的表现.
3.行的长度(Line Length)
一般来说一行的长度最好不要超过80个字符,太长了并不利益阅读.如果一行代码太长了,就另起一行.理想的是在逗号后面另起一行.操作符也行.下一行的缩进是8个字符.
4.备注(Comments)
备注要保持up-to-date.不对的过期的备注还不如不备注,会导致阅读的时候带来更大的误解.一般一行简单的代码都是不需要备注的.只有当代码相对比较复杂了,或者做了某件事情不是很明显的时候,加上备注会提升阅读的质量.
5.变量的声明(Variable Declaration)
所有的变量在使用之前都必须声明,变量的声明只能通过var.不加var的"变量"其实是全局对象(global object)的一个属性.(变量不能被delete,对象的属性可以通过delete操作符delete).
最好的最优雅的写法是每个变量都有他自己单独的一行.按照字母顺序排序,并且变量的声明能在top of the function.全局变量尽量避免使用.
6.函数声明(Function Declaration)
所有的函数都必须在使用之前定义.Inner function应该在var statement之后,这样可以很明白的确定当前scope有哪些变量.
函数名和参数的左边括号"("之间不应该有空格,参数的有括号")"和"{"之间应该有一个空格.函数体内的code应该遵循缩进的规则.最后的"}"应该和声明函数的function齐平.
function outer(a, b) {
var e = a * b;
function inner(c, d) {
return (e * c) + d;
}
return inner(0, 1);
}
对于把函数需要立刻执行的情况,整个被引用的表达式应该都被()包围着,这样就能很明确的表达这个表达式的结果是函数的返回值而不是函数本身.
var collection = (function () {
var keys = [], values = [];
return {
get: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
return values[at];
}
},
set: function (key, value) {
var at = keys.indexOf(key);
if (at < 0) {
at = keys.length;
}
keys[at] = key;
values[at] = value;
},
remove: function (key) {
var at = keys.indexOf(key);
if (at >= 0) {
keys.splice(at, 1);
values.splice(at, 1);
}
}
};
}());
7.命名(Name)
name应该由26个小写的or大写的字符(a-z, A-Z),10位数字(0-9),还有"_"组成.不要用"_"做第一个字符,因为一般来说以"_"开头意味着这个变量是私有的,但是javascript并没有私有的属性这个概念.如果要用私有的就用私有成员属性,并且用特权函数访问这些属性.基本上所有的函数和变量都以小写的字符开头.
8.表达式(statement)
单一的表达式的必须要;结尾,如果不写javascript也会默认加上;
组合的表达式.组合表达式是{....}表示的表达式.
.表达式内部遵照惯用的缩进策略.
.{(左边的大括号)必须在将成为表达式的行的最后.
.}(右边的大括号)必须另起一行,并且和{对齐.
return statement,应该只有值,不要用()包围值,另外如果返回的是个表达式,记住千万要保持表达式和return在同一行,分行的话会导致错误,javascript会默认加上";" 组合表达式的话保证 {和return在同一行.
if statement:
if (condition) {
statements
}
if (condition) {
statements
} else {
statements
}
if (condition) {
statements
} else if (condition) {
statements
} else {
statements
}
for statement:
for (initialization; condition; update) {
statements
}
for (variable in object) {
if (filter) {
statements
}
}
while statement:
while (condition) {
statements
}
do statement:
do {
statements
} while (condition);
switch statement:
switch (expression) {
case expression:
statements
default:
statements
}
case保持和switch对齐
try statement:
try {
statements
} catch (variable) {
statements
}
try {
statements
} catch (variable) {
statements
} finally {
statements
}
尽量避免使用continue,他会改变函数的控制流程.
10.空格(Whitespace)
空白的行能使逻辑相关的代码在一个范围呢,这样有助于提高阅读性.
空格应该按照下面列出来的方面使用:
.关键字和"("之间应该有一个空格.
.调用函数的时候不要加空格.
.所有的二进制操作符除了"." and "(" and "["都要在操作数前加空格.
.所有的单元操作符都不要加空格.
.在for的控制部分依次按照";"顺序加上空格.
.每个,后面都要有空格.
额外建议
.尽量少用new,创建对象的时候用{}代替new object(),创建数组的时候用[]代替new Array()
.如果成员的名字是连续的数字用arrays,如果成员的名字任意string或integer用objects.
.javscript没有block scope.只有function scope.
.不要在if或者while的控制部分进行赋值操作:
if (a = b) {
.尽量使用===和!==进行比较,如果只进行类型比较的话组合typeof和==.
.不要在+操作符后面使用+和++,这样做会造成很打的困扰,如果必须加就在他们之间加上().
total = subtotal + +myInput.value;
is better written as
total = subtotal + (+myInput.value);
eval is
Evil
eval has aliases. Do not use the Function constructor. Do not pass strings to setTimeout or setInterval.
分享到:
相关推荐
Code Conventions for the JavaScript Programming Language Code Conventions for the JavaScript Programming Language Code Conventions for the JavaScript Programming Language Code Conventions for the ...
The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 ISBN 0-13-110362-8 (paperback) ISBN 0-13-110370-9 目录结构: Contents Preface Preface ...
• Code conventions improve the readability of the software, allowing engineers to understand new code more quickly and thoroughly. • If you ship your source code as a product, you need to make sure ...
The C programming Language 第二版英文版 內容列表 Table of Contents Preface.......................................................... Preface to the first edition.......................................
idea阿里代码格式化文件Aliyun Code Conventions.xml,通过idea->editor->scheme中的 Import Scheme-> IntelliJ IDEA code style XML导入
Java语言编码规范 Java Code Conventions
The C programming Language.chm <br>The C programming Language By Brian W. Kernighan and Dennis M. Ritchie. Published by Prentice-Hall in 1988 <br>ISBN 0-13-110362-8 (paperback) ISBN 0...
regarding the conventions, keywords, and terms used with the C language. This chapter attempts to answer some of the most frequently asked questions regarding these subjects. For instance, one of the ...
1 介绍(Introduction) 2 文件名(File Names) 3 文件组织(File ...9 命名规范(Naming Conventions) 10 编程惯例(Programming Practices) 11 代码范例(Code Examples) 花点时间看看这个,让你的代码更清晰可读。
Database Code Conventions
It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that ...
Java语言编码规范(Java Code Conventions)
软件编码规范-CodeConventions_V1.0.0.pdf
Establish specific code conventions for your team Use tools such as JSLint and JSHint to keep your team on track Adopt style guidelines, such as basic formatting, to help your team produce uniform ...
his document contains details about data representation, function calling conventions, register usage conventions, name mangling schemes, etc.
18 Language support library 18.1 General 18.2 Types 18.3 Implementation properties 18.4 Integer types 18.5 Start and termination 18.6 Dynamic memory management CONTENTS v c ISO/IEC N3000=09-...
We’ll look at the language, the tools, and the conventions., Part 2 uses these skills to start writing concurrent code—applications that use all the cores on your machine, or all the machines on ...
Just as Strunk and White's The Elements of Style provides rules for writing in the English language, this book provides conventions for formatting, naming, documentation, programming and testing....