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. 谈Mysql索引

    myisam和innodb的索引有什么区别? 两个索引都是B+树索引,但是myisam的表存储和索引存储是分开的,索引存储中存放的是表的地址.而innodb表存储本身就是一个B+树,它是用主键来做B+ ...

  2. MySQL体系结构以及各种文件类型学习

    1,mysql体系结构 由数据库和数据库实例组成,是单进场多线程架构. 数据库:物理操作系统文件或者其它文件的集合,在mysql中,数据库文件可以是frm.myd.myi.ibd等结尾的文件,当使用n ...

  3. IntelliTrace简介

    解决无法复现bug所使用的策略是在遇到bug时捕获尽可能多的信息,在使用IntelliTrace进行调试时可以充分利用这些信息.最令人称道的一个功能在于bug本身可以自动修复. 打开IntelliTr ...

  4. jquery.ajax 跨域请求webapi,设置headers

    解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头.正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现. 1.第一步 服务端 ...

  5. 分享一个C#的分页类

    废话不说只有代码: using System.Linq; using System.Collections.Generic; namespace CommonLibrary { public clas ...

  6. SQL Server 文件和文件组

    文件和文件组简介 在SQL Server中,数据库在硬盘上的存储方式和普通文件在Windows中的存储方式没有什么不同,仅仅是几个文件而已.SQL Server通过管理逻辑上的文件组的方式来管理文件. ...

  7. mssql server提示无权限

    mssqlserver在查询系统视图时(如:select * from sys.syscacheobjects),有时会报出如下提示: 消息 300,级别 14,状态 1,第 1 行VIEW SERV ...

  8. ASP.NET中Session简单原理图

    刚学习Session,对session的理解相当肤浅,按照我的想法画了原理图,麻烦各位大神指正,谢了!

  9. 为C1Chart for WPF添加自定义标题、坐标轴单位标签以及旋转坐标轴注释

    对于图表控件C1Chart for WPF,我们在添加数据,选择图表类型这些基本可视化数据展示后,经常需要通过标题.坐标轴单位标签等信息辅助说明图表对实际场景的意义.C1Chart for WPF并没 ...

  10. 【洛谷 p3368】模板-树状数组 2(数据结构)

    题目:已知一个数列,你需要进行下面两种操作:1.将某区间每一个数数加上x:2.求出某一个数的和. 解法:树状数组+前缀和优化.数组中每位存和前一位的数的差,这样区间修改只用改两位,单点询问就是求前缀和 ...