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 ...
随机推荐
- css实现高度不固定的div元素模块在页面中水平垂直居中
<!DOCTYPE html><html> <head> <title>Laravel</title> <link ...
- 终极事务处理(XTP,Hekaton)——万能大招?
在SQL Server 2014里,微软引入了终极事务处理(Extreme Transaction Processing),即大家熟知的Hekaton.我在网上围观了一些文档,写这篇文章,希望可以让大 ...
- Scrum 项目4.0--软件工程
1.准备看板. 2.任务认领,并把认领人标注在看板上的任务标签上. 林宇粲:处理数据的存储:目前先进行数据库表的分析和创建. 蔡舜:对复利计算,单利计算,代码进行编写. 王昕明:编写一些用户登录,操作 ...
- PHP入门:在Windows中安装PHP工作环境
PHP入门:在Windows系统中分别安装PHP工作环境 一.什么是LAMP? Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是 ...
- Python入门笔记(22):Python函数(5):变量作用域与闭包
一.全局变量与局部变量 一个模块中,最高级别的变量有全局作用域. 全局变量一个特征就是:除非被删除,否则他们存活到脚本运行结束,且对于所有的函数都可访问. 当搜索一个标识符(也称变量.名字等),Pyt ...
- 10个出色的NoSQL数据库
http://www.infoq.com/research/nosql-databases?utm_source=infoqresearch&utm_campaign=lr-homepage ...
- mysql学习笔记 第八天
where,group by,having重新详解 where的用法: where与in的配合使用,in(值1,值2,...)表示结果在值1,值2,...其中任何一个. 聚合函数和group by的用 ...
- 快速设置超炫banner,js插件
http://www.themepunch.com/codecanyon/revolution_wp/ 记录一下以后用 //出自http://www.cnblogs.com/ahjesus 尊重作者辛 ...
- 浅谈一下缓存策略以及memcached 、redis区别
缓存策略三要素:缓存命中率 缓存更新策略 最大缓存容量.衡量一个缓存方案的好坏标准是:缓存命中率.缓存命中率越高,缓存方法设计的越好. 三者之间的关系为:当缓存到达最大的缓存容量时,会触发缓存更 ...
- Python数学运算的一个小算法(求一元二次方程的实根)
请定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax² + bx + c = 0的两个解. #!/usr/bin/env python # -*- coding: ...