* [18.1](https://github.com/yuche/javascript#18.1)?使用 2 個空格作為縮進。
~~~
// bad
function() {
????const name;
}
// bad
function() {
?const name;
}
// good
function() {
??const name;
}
~~~
* [18.2](https://github.com/yuche/javascript#18.2)?在花括號前放一個空格。
~~~
// bad
function test(){
console.log('test');
}
// good
function test() {
console.log('test');
}
// bad
dog.set('attr',{
age: '1 year',
breed: 'Bernese Mountain Dog',
});
// good
dog.set('attr', {
age: '1 year',
breed: 'Bernese Mountain Dog',
});
~~~
* [18.3](https://github.com/yuche/javascript#18.3)?在控制語句(`if`、`while`?等)的小括號前放一個空格。在函數調用及聲明中,不在函數的參數列表前加空格。
~~~
// bad
if(isJedi) {
fight ();
}
// good
if (isJedi) {
fight();
}
// bad
function fight () {
console.log ('Swooosh!');
}
// good
function fight() {
console.log('Swooosh!');
}
~~~
* [18.4](https://github.com/yuche/javascript#18.4)?使用空格把運算符隔開。
~~~
// bad
const x=y+5;
// good
const x = y + 5;
~~~
* [18.5](https://github.com/yuche/javascript#18.5)?在文件末尾插入一個空行。
~~~
// bad
(function(global) {
// ...stuff...
})(this);
~~~
~~~
// bad
(function(global) {
// ...stuff...
})(this);?
?
~~~
~~~
// good
(function(global) {
// ...stuff...
})(this);?
~~~
* [18.5](https://github.com/yuche/javascript#18.5)?在使用長方法鏈時進行縮進。使用前面的點?`.`?強調這是方法調用而不是新語句。
~~~
// bad
$('#items').find('.selected').highlight().end().find('.open').updateCount();
// bad
$('#items').
find('.selected').
highlight().
end().
find('.open').
updateCount();
// good
$('#items')
.find('.selected')
.highlight()
.end()
.find('.open')
.updateCount();
// bad
const leds = stage.selectAll('.led').data(data).enter().append('svg:svg').class('led', true)
.attr('width', (radius + margin) * 2).append('svg:g')
.attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.call(tron.led);
// good
const leds = stage.selectAll('.led')
.data(data)
.enter().append('svg:svg')
.classed('led', true)
.attr('width', (radius + margin) * 2)
.append('svg:g')
.attr('transform', 'translate(' + (radius + margin) + ',' + (radius + margin) + ')')
.call(tron.led);
~~~
* [18.6](https://github.com/yuche/javascript#18.6)?在塊末和新語句前插入空行。
~~~
// bad
if (foo) {
return bar;
}
return baz;
// good
if (foo) {
return bar;
}
return baz;
// bad
const obj = {
foo() {
},
bar() {
},
};
return obj;
// good
const obj = {
foo() {
},
bar() {
},
};
return obj;
~~~
- 關于
- 1. 類型
- 2. 引用
- 3. 對象
- 4. 數組
- 5. 解構
- 6. 字符串
- 7. 函數
- 8. 箭頭函數
- 9. 構造函數
- 10. 模塊
- 11. Iterators & Generators
- 12. 屬性
- 13. 變量
- 14. 提升
- 15. 比較運算符 & 等號
- 16. 代碼塊
- 17. 注釋
- 18. 空白
- 19. 逗號
- 20. 分號
- 21. 類型轉換
- 22. 命名規則
- 23. 存取器
- 24. 事件
- 25. jQuery
- 26. ECMAScript 5 兼容性
- 27. ECMAScript 6 編碼規范
- 28. 測試
- 29. 性能
- 30. 資源
- 31. 使用人群
- 32. 翻譯
- 33. JavaScript 編碼規范說明
- 34. 一起來討論Javascript
- 35. Contributors
- 36. License