* [6.1](https://github.com/yuche/javascript#6.1)?字符串使用單引號?`''`?。
~~~
// bad
const name = "Capt. Janeway";
// good
const name = 'Capt. Janeway';
~~~
* [6.2](https://github.com/yuche/javascript#6.2)?字符串超過 80 個字節應該使用字符串連接號換行。
* [6.3](https://github.com/yuche/javascript#6.3)?注:過度使用字串連接符號可能會對性能造成影響。[jsPerf](http://jsperf.com/ya-string-concat)?和?[討論](https://github.com/airbnb/javascript/issues/40).
~~~
// bad
const 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
const 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
const 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.';
~~~
* [6.4](https://github.com/yuche/javascript#6.4)?程序化生成字符串時,使用模板字符串代替字符串連接。
> 為什么?模板字符串更為簡潔,更具可讀性。
~~~
// bad
function sayHi(name) {
return 'How are you, ' + name + '?';
}
// bad
function sayHi(name) {
return ['How are you, ', name, '?'].join();
}
// good
function sayHi(name) {
return `How are you, ${name}?`;
}
~~~
- 關于
- 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