var len = items.length;
var itemsCopy = [];
var i;
// bad
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
// good
itemsCopy = items.slice();
Demo
// bad
var a = [1,2,3];
var b = a;
console.log(a);// => [1,2,3]
console.log(b);// => [1,2,3]
a[0]=4;
console.log(a);// => [4,2,3]
console.log(b);// => [4,2,3]
// bad
var a = [1,2,3];
var b = [];
console.log(a);// => [1,2,3]
console.log(b);// => []
for(var i=0; i<a.length; i++){b[i]=a[i]}
console.log(a);// => [1,2,3]
console.log(b);// => [1,2,3]
a[0]=4;
console.log(a);// => [4,2,3]
console.log(b);// => [1,2,3]
// good
var a = [1,2,3];
var b = a.slice();
console.log(a);// => [1,2,3]
console.log(b);// => [1,2,3]
a[0]=4;
console.log(a);// => [4,2,3]
console.log(b);// => [1,2,3]
function trigger() {
var args = Array.prototype.slice.call(arguments);
...
}
Demo
function trigger() {
var args = Array.prototype.slice.call(arguments);
return args
}
function trigger2() {
return arguments
}
// bad
a = trigger2('a','b','c','d') // 此时a为类数组对象
a.push('e');// => Uncaught TypeError: a.push is not a function
// good
a = trigger('a','b','c','d') // 此时a为数组对象
a.push('e');
console.log(a);// => ["a", "b", "c", "d", "e"]
2.4 字符串
使用单引号''包裹字符串。
// bad
var name = "Bob Parr";
// good
var name = 'Bob Parr';
// bad
var fullName = "Bob " + this.lastName;
// good
var fullName = 'Bob ' + this.lastName;
// bad
var errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
// bad
var errorMessage = 'This is a super long error that was thrown because \
of Batman. When you stop to think about how Batman had anything to do \
with this, you would get nowhere \
fast.';
// good
var errorMessage = 'This is a super long error that was thrown because ' +
'of Batman. When you stop to think about how Batman had anything to do ' +
'with this, you would get nowhere fast.';
var items;
var messages;
var length;
var i;
messages = [{
state: 'success',
message: 'This one worked.'
}, {
state: 'success',
message: 'This one worked as well.'
}, {
state: 'error',
message: 'This one did not work.'
}];
length = messages.length;
// bad
function inbox(messages) {
items = '<ul>';
for (i = 0; i < length; i++) {
items += '<li>' + messages[i].message + '</li>';
}
return items + '</ul>';
}
// good
function inbox(messages) {
items = [];
for (i = 0; i < length; i++) {
// use direct assignment in this case because we're micro-optimizing.
items[i] = '<li>' + messages[i].message + '</li>';
}
return '<ul>' + items.join('') + '</ul>';
}
2.5 函数
函数表达式:
// 匿名函数表达式
var anonymous = function() {
return true;
};
// 命名函数表达式
var named = function named() {
return true;
};
// 立即调用的函数表达式(IIFE)
(function () {
console.log('Welcome to the Internet. Please follow me.');
}());
// bad
if (currentUser) {
function test() {
console.log('Nope.');
}
}
// good
var test;
if (currentUser) {
test = function test() {
console.log('Yup.');
};
}
永远不要把参数命名为arguments。将这取代函数作用英文域内的arguments对象。
// bad
function nope(name, options, arguments) {
// ...stuff...
}
// good
function yup(name, options, args) {
// ...stuff...
}
2.6 属性
使用.来访问对象的属性。
var luke = {
jedi: true,
age: 28
};
// bad
var isJedi = luke['jedi'];
// good
var isJedi = luke.jedi;
当通过变量访问属性时使用中括号[]。
var luke = {
jedi: true,
age: 28
};
function getProp(prop) {
return luke[prop];
}
var isJedi = getProp('jedi');
2.7 变量
使用总是var来声明变量。不这么做将导致产生全局变量。我们要避免污染全局命名空间。
// bad
superPower = new SuperPower();
// good
var superPower = new SuperPower();
使用var声明每一个变量。
这样做的好处是增加新变量将变的更加容易,你而且永远不用再担心调换错;跟,。
// bad
var items = getItems(),
goSportsTeam = true,
dragonball = 'z';
// bad
// (跟上面的代码比较一下,看看哪里错了)
var items = getItems(),
goSportsTeam = true;
dragonball = 'z';
// good
var items = getItems();
var goSportsTeam = true;
var dragonball = 'z';
最后再声明未赋值的变量。当你需要引用前面的变量赋值时这将变的很有用。
// bad
var i, len, dragonball,
items = getItems(),
goSportsTeam = true;
// bad
var i;
var items = getItems();
var dragonball;
var goSportsTeam = true;
var len;
// good
var items = getItems();
var goSportsTeam = true;
var dragonball;
var length;
var i;
在作用域顶部声明变量。这将帮你避免变量声明提升相关的问题。
// bad
function () {
test();
console.log('doing stuff..');
//..other stuff..
var name = getName();
if (name === 'test') {
return false;
}
return name;
}
// good
function () {
var name = getName();
test();
console.log('doing stuff..');
//..other stuff..
if (name === 'test') {
return false;
}
return name;
}
// bad - 不必要的函数调用
function () {
var name = getName();
if (!arguments.length) {
return false;
}
this.setFirstName(name);
return true;
}
// good
function () {
var name;
if (!arguments.length) {
return false;
}
name = getName();
this.setFirstName(name);
return true;
}