# 29. Coding style tips for ECMAScript 6
This chapter lists a few ideas related to ES6 coding style:
- `var` versus `let` versus `const` (details are explained in [the chapter on variables](ch_variables.html#const-vs-let-vs-var)):
- Prefer `const`. You can use it for all variables whose values never change.
- Otherwise, use `let` – for variables whose values do change.
- Avoid `var`.
- An arrow function is the superior solution whenever a function fits into a single line: `` `readFilePromisified``(``filename``)`
`.``then``(``text` `=>` `console``.``log``(``text``))`
For multi-line functions, traditional functions work well, too (with the caveat of `this` not being lexical):
`` `readFilePromisified``(``filename``)`
`.``then``(``function` `(``text``)` `{`
`const` `obj` `=` `JSON``.``parse``(``text``);`
`console``.``log``(``JSON``.``stringify``(``obj``,` `null``,` `4``));`
`});`
Single-line functions tend to be throw-away. If a function isn’t then a traditional function has the advantage that you can name it, which is useful for documentation and debugging.
- Properties in object literals: As soon as an object literal spans multiple lines, I add a comma after the last entry. Such a trailing comma has been legal since ES5. It makes adding, removing and rearranging entries simpler. As a consequence, method definitions always end with `},`: `` `const` `obj` `=` `{`
`foo``()` `{`
`},`
`bar``()` `{`
`},`
`};`
- Modules: don’t mix default exports and named exports. Your module should either specialize on a single thing or export multiple, named, things. Details are explained in [the chapter on modules](ch_modules.html#sec_mixing-named-and-default-exports).
- Format generators as follows: `` `// Generator function declaration`
`function``*` `genFunc``()` `{` `···` `}`
`// Generator function expression`
`const` `genFunc` `=` `function``*` `()` `{` `···` `};`
`// Generator method definition in an object literal`
`const` `obj` `=` `{`
`*` `generatorMethod``()` `{`
`···`
`}`
`};`
`// Generator method definition in a class definition`
`class` `MyClass` `{`
`*` `generatorMethod``()` `{`
`···`
`}`
`}`
Details are explained in [the chapter on generators](ch_generators.html#sec_formating-generators).
- The chapter on parameter handling has [style tips for function signatures](ch_parameter-handling.html#sec_parameter-handling-style-tips): `` `// Mark optional parameters via the parameter default value `undefined``
`function` `foo``(``optional` `=` `undefined``)` `{` `···` `}`
`// Mark required parameters via a function that throws an exception`
`function` `foo``(``required` `=` `throwException``())` `{` `···` `}`
`// Enforcing a maximum arity (variant 1 of 2)`
`function` `f``(``x``,` `y``,` `...``empty``)` `{` `// max arity: 2`
`if` `(``empty``.``length` `>` `0``)` `{`
`throw` `new` `Error``();`
`}`
`}`
`// Enforcing a maximum arity (variant 2 of 2)`
`function` `f``(``x``,` `y``)` `{` `// max arity: 2`
`if` `(``arguments``.``length` `>` `2``)` `{`
`throw` `new` `Error``();`
`}`
`}`
- In [the chapter on callable entities](ch_callables.html#sec_callables-style) (traditional functions, arrow functions, classes, etc.) there is a section that gives recommendations (when to use which one etc.).
Additionally, the [ES5 coding style tips](http://speakingjs.com/es5/ch26.html) in “Speaking JavaScript” are still relevant for ES6.
Next: [30. An overview of what’s new in ES6](ch_overviews.html)
- 关于本书
- 目录简介
- 关于这本书你需要知道的
- 序
- 前言
- I 背景
- 1. About ECMAScript 6 (ES6)
- 2. 常见问题:ECMAScript 6
- 3. 一个JavaScript:在 ECMAScript 6 中避免版本化
- 4. 核心ES6特性
- II 数据
- 5. New number and Math features
- 6. 新的字符串特性
- 7. Symbol
- 8. Template literals
- 第9章 变量与作用域
- 第10章 解构
- 第11章 参数处理
- III 模块化
- 12. ECMAScript 6中的可调用实体
- 13. 箭头函数
- 14. 除了类之外的新OOP特性
- 15. 类
- 16. 模块
- IV 集合
- 17. The for-of loop
- 18. New Array features
- 19. Maps and Sets
- 20. 类型化数组
- 21. 可迭代对象和迭代器
- 22. 生成器( Generator )
- V 标准库
- 23. 新的正则表达式特性
- 24. 异步编程 (基础知识)
- 25. 异步编程的Promise
- VI 杂项
- 26. Unicode in ES6
- 27. 尾部调用优化
- 28 用 Proxy 实现元编程
- 29. Coding style tips for ECMAScript 6
- 30. 概述ES6中的新内容
- 注释
- ES5过时了吗?
- ==个人笔记==