// 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.';
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 functioninbox(messages) { items = '<ul>';
for (i = 0; i < length; i++) { items += '<li>' + messages[i].message + '</li>'; }
return items + '</ul>'; }
// good functioninbox(messages) { items = [];
for (i = 0; i < length; i++) { items[i] = '<li>' + messages[i].message + '</li>'; }
return'<ul>' + items.join('') + '</ul>'; } // very good functioninbox(messages) { items = []; for (i = 0, len = messages.length; i < len; i++) { items.push('<li>' + messages[i].message + '</li>';); } return'<ul>' + items.join('') + '</ul>'; }
函数
函数表达式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// anonymous function expression var anonymous = function() { returntrue; };
// named function expression var named = functionnamed() { returntrue; };
// immediately-invoked function expression (IIFE) (function() { console.log('Welcome to the Internet. Please follow me.'); })();
// we know this wouldn't work (assuming there // is no notDefined global variable) functionexample() { console.log(notDefined); // => throws a ReferenceError }
// creating a variable declaration after you // reference the variable will work due to // variable hoisting. Note: the assignment // value of `true` is not hoisted. functionexample() { console.log(declaredButNotAssigned); // => undefined var declaredButNotAssigned = true; }
// The interpreter is hoisting the variable // declaration to the top of the scope, // which means our example could be rewritten as: functionexample() { var declaredButNotAssigned; console.log(declaredButNotAssigned); // => undefined declaredButNotAssigned = true; }
// bad functiongetType() { console.log('fetching type...'); // set the default type to 'no type' var type = this._type || 'no type';
return type; }
// good functiongetType() { console.log('fetching type...');
// set the default type to 'no type' var type = this._type || 'no type';
return type; }
如果你指出的问题需要重新定位或者提出一个待解决的问题需要实现,给注释添加 FIXME or TODO 前缀有利于其他开发者快速理解。这些注释不同于通常的注释,因为它们是可实施的。这些实施措施就是FIXME – need to figure this out or TODO – need to implement.使用 // FIXME: 给一个问题作注释
1 2 3 4 5 6 7
functionCalculator() {
// FIXME: shouldn't use a global here total = 0;
returnthis; }
使用 //TODO: 给问题解决方案作注释
1 2 3 4 5 6 7
functionCalculator() {
// TODO: total should be configurable by an options param this.total = 0;
// bad var hero = { firstName: 'Kevin', lastName: 'Flynn', };
var heroes = [ 'Batman', 'Superman', ];
// good var hero = { firstName: 'Kevin', lastName: 'Flynn' };
var heroes = [ 'Batman', 'Superman' ];
分号
恩,这也是规范一部分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// bad (function() { var name = 'Skywalker' return name })()
// good (function() { var name = 'Skywalker'; return name; })();
// good (guards against the function becoming an argument when two files with IIFEs are concatenated) ;(function() { var name = 'Skywalker'; return name; })();
类型分配&强制转换
执行强制类型转换的语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14
Strings: // => this.reviewScore = 9;
// bad var totalScore = this.reviewScore + '';
// good var totalScore = '' + this.reviewScore;
// bad var totalScore = '' + this.reviewScore + ' total score';
// good var totalScore = this.reviewScore + ' total score';
// good /** * parseInt was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */ var val = inputValue >> 0;