It’s important to establish and follow coding conventions—they make your code consistent, predictable, and much easier to read and understand. A new developer joining the team can read through the conventions and be productive much sooner, understanding the code written by any other team member.

Indentation

The rule is simple—anything within curly braces. This means the bodies of functions, loops (do, while, for, for-in), ifs, switches, and object properties in the object literal notation.

function outer(a, b) {
var c = 1,
d = 2,
inner;
if (a > b) {
inner = function () {
return {
r: c - d
};
};
} else {
inner = function () {
return {
r: c + d
};
};
}
return inner;
}

Curly Braces

Curly braces should always be used, even in cases when they are optional.

// bad practice
for (var i = 0; i < 10; i += 1)
alert(i); // better
for (var i = 0; i < 10; i += 1) {
alert(i);
} Similarly for if conditions:
// bad
if (true)
alert(1);
else
alert(2); // better
if (true) {
alert(1);
} else {
alert(2);
}

Opening Brace Location

semicolon insertion mechanism—JavaScript is not picky when you choose not to end your lines properly with a semicolon and adds it for you.

// warning: unexpected return value

function func() {
return
{
name: "Batman"
};
}

If you expect this function to return an object with a  name property, you’ll be surprised. Because of the implied semicolons, the function returns undefined. The preceding code is equivalent to this one:

// warning: unexpected return value
function func() {
return undefined;
// unreachable code follows...
{
name: "Batman"
};
}

In conclusion, always use curly braces and always put the opening one on the same line as the previous statement:

function func() {
return {
name: "Batman"
};
}

White Space

Good places to use a white space include:

• After the semicolons that separate the parts of a for loop: for example, for (var i= 0; i < 10; i += 1) {...}

• Initializing multiple variables (i and max) in a for loop:  for (var i = 0, max = 10; i < max; i += 1) {...}

• After the commas that delimit array items: var a = [1, 2, 3];

• After commas in object properties and after colons that divide property names and their values: var o = {a: 1, b: 2};

• Delimiting function arguments: myFunc(a, b, c)

• Before the curly braces in function declarations: function myFunc() {}

• After function in anonymous function expressions:  var myFunc = function () {};

Another good use for white space is to separate all operators and their operands with

spaces, which basically means use a space before and after  +,  -,  *,  =,  <,  >,  <=,  >=,  = = =,  != =, &&, ||, +=, and so on:

// generous and consistent spacing makes the code easier to read allowing it to "breathe"
var d = 0,
a = b + 1;
if (a && b && c) {
d = a % c;
a += d;
} // antipattern
// missing or inconsistent spaces make the code confusing
var d= 0,
a =b+1;
if (a&& b&&c) {
d=a %c;
a+= d;
}

And a final note about white space—curly braces spacing. It’s good to use a space:

• Before opening curly braces ({) in functions,  if-else cases, loops, and object literals

• Between the closing curly brace (}) and else or while

JavaScript Patterns 2.9 Coding Conventions的更多相关文章

  1. JavaScript Patterns 2.10 Naming Conventions

    1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the word ...

  2. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  3. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  4. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  5. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  6. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  7. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  8. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  9. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

随机推荐

  1. Android 学习笔记之如何实现简单相机功能

    PS:看来算法和数据结构还是非常有用的,以后每天都练习两道算法题目...这次忘了对代码进行折叠了..导致篇幅过长... 学习内容: 1.Android如何实现相机功能... 2.如何实现音频的录制.. ...

  2. [Solution] NPOI操作Excel

    NPOI 是 POI 项目的 .NET 版本.POI是一个开源的Java读写Excel.WORD等微软OLE2组件文档的项目.使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 ...

  3. 0506团队项目-Scrum 项目1.0

    题目 1.应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 2.录制为演说视频,上传到视频网站,并把链接发到团队博客上. 团队项目选题 金融工具:复利计算与投资记录项目继续升级,开 ...

  4. svn状态图标大全

     黄色感叹号(有冲突):--这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别 ...

  5. 2015 Multi-University Training Contest 1 - 10010 Y sequence

    Y sequence Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=5297 Mean: 有连续数列A={1,2,3,4,5,6, ...

  6. Oracle工程建设行业解决方案

    为何选择Oracle工程建设行业解决方案? Oracle为工程建设企业提供一套全面.开放且集成的业务管理软件.服务器和存储解决方案.这些解决方案经过集成设计,能够实现卓越性能,从而优化业务的方方面面. ...

  7. weblogic 12C 数据源配置出错的解决办法

    驱动程序类名称:   11G 10.3.6与12G数据源配置有很大区别,整个一天才搞明白.   如有疑问可留言:http://www.cnblogs.com/endv/p/4110798.html 配 ...

  8. java多线程(三)——锁机制synchronized(同步语句块)

    用关键字synchronized声明方法在某些情况下是有弊端的,比如A线程调用同步方法之行一个长时间的任务,那么B线程必须等待比较长的时间,在这样的情况下可以使用synchronized同步语句快来解 ...

  9. 二、SQL语句映射文件(1)resultMap

    //备注:该博客引自:http://limingnihao.iteye.com/blog/106076 SQL 映射XML 文件是所有sql语句放置的地方.需要定义一个workspace,一般定义为对 ...

  10. PHP学习笔记:APACHE配置虚拟目录、一个站点使用多域名配置方式

    我用的是xmapp lite2016的集成包,配置虚拟目录教程如下: 找到httpd-vhosts.conf这个文件,这个文件一般是在xampp\apache\conf\extra这个路径下面,找不到 ...