JavaScript Patterns 2.9 Coding Conventions
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的更多相关文章
- JavaScript Patterns 2.10 Naming Conventions
1. Capitalizing Constructors var adam = new Person(); 2. Separating Words camel case - type the word ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- 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 ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
随机推荐
- SystemTap知识(一)
SystemTap是一个系统的跟踪探测工具.它能让用户来跟踪和研究计算机系统在底层的实现. 安装SystemTap需要为你的系统内核安装-devel,-debuginfo,-debuginfo-com ...
- Linux永久修改系统时间和时区方法
修改时区: 1> 找到相应的时区文件 /usr/share/zoneinfo/Asia/Shanghai 用这个文件替换当前的/etc/localtime文件. 或者找你认为是标准时间的服务器, ...
- Sprint第三个冲刺(第二天)
一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:
- 0506团队项目-Scrum 项目1.0
题目 1.应用NABCD模型,分析你们初步选定的项目,充分说明你们选题的理由. 2.录制为演说视频,上传到视频网站,并把链接发到团队博客上. 团队项目选题 金融工具:复利计算与投资记录项目继续升级,开 ...
- java 接口学习
你应该知道接口是一种契约,它与实现方式无关 但是类,即使是抽象类,你都能自定义成员变量,而成员变量往往就与实现方式有关. 这一点的实际意义不大. 但是有一点,类会暴露太多不必要,甚至不能暴露的东西,你 ...
- Fluent Nhibernate and Stored Procedures
sql:存储过程 DROP TABLE Department GO CREATE TABLE Department ( Id INT IDENTITY(1,1) PRIMARY KEY, DepNam ...
- 通过代码的方式完成WCF服务的寄宿工作
使用纯代码的方式进行服务寄宿 服务寄宿的目的是为了开启一个进程,为WCF服务提供一个运行的环境.通过为服务添加一个或者多个终结点,使之暴露给潜在的服务消费,服务消费者通过匹配的终结点对该服务进行调用, ...
- js 自带的 sort() 方法
1. 方法概述 Array的sort()方法默认把所有元素先转换为String再根据Unicode排序, sort()会改变原数组,并返回改变(排序)后的数组 . 2. 例子 2.1 如果没有提供自定 ...
- innerHTML和outerHTML有什么区别
一.区别:1)innerHTML: 从对象的起始位置到终止位置的全部内容,不包括Html标签.2)outerHTML: 除了包含innerHTML的全部内容外, 还包含对象标签本身. 二.例子1: & ...
- php学习笔记:自定义函数的调用
PHP内置了超过1000个函数,因此函数使得PHP成为一门非常强大的语言.大多数时候我们使用系统的内置函数就可以满足需求,但是自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰. ...