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. java基础---->java中正则表达式二

    跟正则表达式相关的类有:Pattern.Matcher和String.今天我们就开始Java中正则表达式的学习. Pattern和Matcher的理解 一.正则表达式的使用方法 一般推荐使用的方式如下 ...

  2. Android学习笔记之使用百度地图实现地图控制

    PS:吾之荣耀,离别已久. 学习内容: 1.实现地图控制. 2.百度地图开发的一些细节     1.实现地图控制:   这一篇主要写在百度地图上添加一些其他控制.上一篇书写了覆盖物的添加,地理编码和反 ...

  3. 数论 - 组合数学 + 素数分解 --- hdu 2284 : Solve the puzzle, Save the world!

    Solve the puzzle, Save the world! Problem Description In the popular TV series Heroes, there is a ta ...

  4. 搜索 --- 数独求解 POJ 2676 Sudoku

    Sudoku Problem's Link:   http://poj.org/problem?id=2676 Mean: 略 analyse: 记录所有空位置,判断当前空位置是否可以填某个数,然后直 ...

  5. Brute Force - B. Candy Boxes ( Codeforces Round #278 (Div. 2)

    B. Candy Boxes Problem's Link:   http://codeforces.com/contest/488/problem/B Mean: T题目意思很简单,不解释. ana ...

  6. Linq之group子句

    在Linq查询语句中,group子句主要作用是对查询的结果集进行分组.并返回元素类型为IGrouping<TKey,TElement>的对象序列. 下面我们在代码实例中创建一个GroupQ ...

  7. 译:c#生成条码的web控件

    译文:http://www.codeproject.com/Tips/846860/Csharp-Barcode-Generator-Web-Control 在asp.net的web页用c#的web控 ...

  8. c#开发工具软件集合

    visual studio 2015(自带Nuget) Resharper de4dot dnspy ILMergeGui Git 大漠插件3.1233 天使插件v4.019 Navicat_Prem ...

  9. display:inline-block兼容ie6/7的写法

    2.display:inline-block作用? 使用display:inline-block属性,可以使行内元素或块元素能够变成行内块元素,简单直白点讲就是不加float属性就可以定义自身的宽.高 ...

  10. CRC16校验码生成

    /// <summary> /// 计算CRC-16 /// </summary> /// <param name="data"></pa ...