# JavaScript Standard Style
编码风格并不会使程序更正确,但是采用编码风格检查工具可以帮助捕获一些错误;
代码的可读性很重要,阅读和尝试理解程序比编写代码要花费更多的精力和时间,因为请为未来的自己和维护代码的其它同事进行可读性优化。
人生苦短,不能因个人偏好的不同而引起意见分歧;标准的编码风格能够避免团队之间的分歧和摩擦;多人协作代码风格统一利于后期的维护和阅读,也能作为代码审查的依据之一。
你可以不用任何工具就可以采用这个标准,只要通读一下 规则 (opens new window) 就可以
# 实施方法:
针对喜欢的编辑器安装编辑器插件自动化格式化,自动检查代码风格。
# 安装方法:
https://standardjs.com/readme-zhcn.html
掌握本规范的最好方法是安装并在自己的代码中使用它。
# JavaScript 标准风格细则
- 使用两个空格进行缩进。 - eslint: - indent(opens new window)- function hello (name) { console.log('hi', name) }1
 2
 3
- 除需要转义的情况外,字符串统一使用单引号。 - eslint: - quotes(opens new window)- console.log('hello there') $("<div class='box'>")1
 2
- 不要定义未使用的变量。 - eslint: - no-unused-vars(opens new window)- function myFunction () { var result = something() // ✗ avoid }1
 2
 3
- 关键字后面加空格。 - eslint: - keyword-spacing(opens new window)- if (condition) { ... } // ✓ ok if(condition) { ... } // ✗ avoid1
 2
- 函数声明时括号与函数名间加空格。 - eslint: - space-before-function-paren(opens new window)- function name (arg) { ... } // ✓ ok function name(arg) { ... } // ✗ avoid run(function () { ... }) // ✓ ok run(function() { ... }) // ✗ avoid1
 2
 3
 4
 5
- 始终使用 - ===替代- ==。 例外:- obj == null可以用来检查- null || undefined。- eslint: - eqeqeq(opens new window)- if (name === 'John') // ✓ ok if (name == 'John') // ✗ avoid1
 2- if (name !== 'John') // ✓ ok if (name != 'John') // ✗ avoid1
 2
- 字符串拼接操作符 (Infix operators) 之间要留空格。 - eslint: - space-infix-ops(opens new window)- // ✓ ok var x = 2 var message = 'hello, ' + name + '!'1
 2
 3- // ✗ avoid var x=2 var message = 'hello, '+name+'!'1
 2
 3
- 逗号后面加空格。 - eslint: - comma-spacing(opens new window)- // ✓ ok var list = [1, 2, 3, 4] function greet (name, options) { ... }1
 2
 3- // ✗ avoid var list = [1,2,3,4] function greet (name,options) { ... }1
 2
 3
- else 关键字要与花括号保持在同一行。 - eslint: - brace-style(opens new window)- // ✓ ok if (condition) { // ... } else { // ... }1
 2
 3
 4
 5
 6- // ✗ avoid if (condition) { // ... } else { // ... }1
 2
 3
 4
 5
 6
 7
 8
 9
- 多行 if 语句的的括号不能省。 - eslint: - curly(opens new window)- // ✓ ok if (options.quiet !== true) console.log('done')1
 2- // ✓ ok if (options.quiet !== true) { console.log('done') }1
 2
 3
 4- // ✗ avoid if (options.quiet !== true) console.log('done')1
 2
 3
- 不要丢掉异常处理中 - err参数。- eslint: - handle-callback-err(opens new window)- // ✓ ok run(function (err) { if (err) throw err window.alert('done') })1
 2
 3
 4
 5- // ✗ avoid run(function (err) { window.alert('done') })1
 2
 3
 4
- 使用浏览器全局变量时加上 - window.前缀。 Exceptions are:- document,- consoleand- navigator.- eslint: - no-undef(opens new window)- window.alert('hi') // ✓ ok1
- 不允许有连续多行空行。 - eslint: - no-multiple-empty-lines(opens new window)- // ✓ ok var value = 'hello world' console.log(value)1
 2
 3- // ✗ avoid var value = 'hello world' console.log(value)1
 2
 3
 4
 5
- 对于三元运算符 - ?和- :与他们所负责的代码处于同一行- eslint: - operator-linebreak(opens new window)- // ✓ ok var location = env.development ? 'localhost' : 'www.api.com' // ✓ ok var location = env.development ? 'localhost' : 'www.api.com' // ✗ avoid var location = env.development ? 'localhost' : 'www.api.com'1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
- 每个 var 关键字单独声明一个变量。 - eslint: - one-var(opens new window)- // ✓ ok var silent = true var verbose = true // ✗ avoid var silent = true, verbose = true // ✗ avoid var silent = true, verbose = true1
 2
 3
 4
 5
 6
 7
 8
 9
 10
- 条件语句中赋值语句使用括号包起来。这样使得代码更加清晰可读,而不会认为是将条件判断语句的全等号( - ===)错写成了等号(- =)。- eslint: - no-cond-assign(opens new window)- // ✓ ok while ((m = text.match(expr))) { // ... } // ✗ avoid while (m = text.match(expr)) { // ... }1
 2
 3
 4
 5
 6
 7
 8
 9
- 单行代码块两边加空格。 - eslint: - block-spacing(opens new window)- function foo () {return true} // ✗ avoid function foo () { return true } // ✓ ok1
 2
- 对于变量和函数名统一使用驼峰命名法。 - eslint: - camelcase(opens new window)- function my_function () { } // ✗ avoid function myFunction () { } // ✓ ok var my_var = 'hello' // ✗ avoid var myVar = 'hello' // ✓ ok1
 2
 3
 4
 5
- 不允许有多余的行末逗号。 - eslint: - comma-dangle(opens new window)- var obj = { message: 'hello', // ✗ avoid }1
 2
 3
- 始终将逗号置于行末。 - eslint: - comma-style(opens new window)- var obj = { foo: 'foo' ,bar: 'bar' // ✗ avoid } var obj = { foo: 'foo', bar: 'bar' // ✓ ok }1
 2
 3
 4
 5
 6
 7
 8
 9
- 点号操作符须与属性需在同一行。 - eslint: - dot-location(opens new window)- console. log('hello') // ✗ avoid console .log('hello') // ✓ ok1
 2
 3
 4
 5
- 文件末尾留一空行。 - eslint: - eol-last(opens new window)
- 函数调用时标识符与括号间不留间隔。 - eslint: - func-call-spacing(opens new window)- console.log ('hello') // ✗ avoid console.log('hello') // ✓ ok1
 2
- 键值对当中冒号与值之间要留空白。 - eslint: - key-spacing(opens new window)- var obj = { 'key' : 'value' } // ✗ avoid var obj = { 'key' :'value' } // ✗ avoid var obj = { 'key':'value' } // ✗ avoid var obj = { 'key': 'value' } // ✓ ok1
 2
 3
 4
- 构造函数要以大写字母开头。 - eslint: - new-cap(opens new window)- function animal () {} var dog = new animal() // ✗ avoid function Animal () {} var dog = new Animal() // ✓ ok1
 2
 3
 4
 5
- 无参的构造函数调用时要带上括号。 - eslint: - new-parens(opens new window)- function Animal () {} var dog = new Animal // ✗ avoid var dog = new Animal() // ✓ ok1
 2
 3
- 对象中定义了存值器,一定要对应的定义取值器。 - eslint: - accessor-pairs(opens new window)- var person = { set name (value) { // ✗ avoid this._name = value } } var person = { set name (value) { this._name = value }, get name () { // ✓ ok return this._name } }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
- 子类的构造器中一定要调用 super - eslint: - constructor-super(opens new window)- class Dog { constructor () { super() // ✗ avoid } } class Dog extends Mammal { constructor () { super() // ✓ ok } }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
- 使用数组字面量而不是构造器。 - eslint: - no-array-constructor(opens new window)- var nums = new Array(1, 2, 3) // ✗ avoid var nums = [1, 2, 3] // ✓ ok1
 2
- 避免使用 arguments.callee 和 arguments.caller。 - eslint: - no-caller(opens new window)- function foo (n) { if (n <= 0) return arguments.callee(n - 1) // ✗ avoid } function foo (n) { if (n <= 0) return foo(n - 1) }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
- 避免对类名重新赋值。 - eslint: - no-class-assign(opens new window)- class Dog {} Dog = 'Fido' // ✗ avoid1
 2
- 避免修改使用 const 声明的变量。 - eslint: - no-const-assign(opens new window)- const score = 100 score = 125 // ✗ avoid1
 2
- 避免使用常量作为条件表达式的条件(循环语句除外)。 - eslint: - no-constant-condition(opens new window)- if (false) { // ✗ avoid // ... } if (x === 0) { // ✓ ok // ... } while (true) { // ✓ ok // ... }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
- 正则中不要使用控制符。 - eslint: - no-control-regex(opens new window)- var pattern = /\x1f/ // ✗ avoid var pattern = /\x20/ // ✓ ok1
 2
- 不要使用 debugger。 - eslint: - no-debugger(opens new window)- function sum (a, b) { debugger // ✗ avoid return a + b }1
 2
 3
 4
- 不要对变量使用 delete 操作。 - eslint: - no-delete-var(opens new window)- var name delete name // ✗ avoid1
 2
- 不要定义冗余的函数参数。 - eslint: - no-dupe-args(opens new window)- function sum (a, b, a) { // ✗ avoid // ... } function sum (a, b, c) { // ✓ ok // ... }1
 2
 3
 4
 5
 6
 7
- 类中不要定义冗余的属性。 - eslint: - no-dupe-class-members(opens new window)- class Dog { bark () {} bark () {} // ✗ avoid }1
 2
 3
 4
- 对象字面量中不要定义重复的属性。 - eslint: - no-dupe-keys(opens new window)- var user = { name: 'Jane Doe', name: 'John Doe' // ✗ avoid }1
 2
 3
 4
- switch 语句中不要定义重复的 case 分支。 - eslint: - no-duplicate-case(opens new window)- switch (id) { case 1: // ... case 1: // ✗ avoid }1
 2
 3
 4
 5
- 同一模块有多个导入时一次性写完。 - eslint: - no-duplicate-imports(opens new window)- import { myFunc1 } from 'module' import { myFunc2 } from 'module' // ✗ avoid import { myFunc1, myFunc2 } from 'module' // ✓ ok1
 2
 3
 4
- 正则中不要使用空字符。 - eslint: - no-empty-character-class(opens new window)- const myRegex = /^abc[]/ // ✗ avoid const myRegex = /^abc[a-z]/ // ✓ ok1
 2
- 不要解构空值。 - eslint: - no-empty-pattern(opens new window)- const { a: {} } = foo // ✗ avoid const { a: { b } } = foo // ✓ ok1
 2
- 不要使用 eval()。 - eslint: - no-eval(opens new window)- eval( "var result = user." + propName ) // ✗ avoid var result = user[propName] // ✓ ok1
 2
- catch 中不要对错误重新赋值。 - eslint: - no-ex-assign(opens new window)- try { // ... } catch (e) { e = 'new value' // ✗ avoid } try { // ... } catch (e) { const newVal = 'new value' // ✓ ok }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
- 不要扩展原生对象。 - eslint: - no-extend-native(opens new window)- Object.prototype.age = 21 // ✗ avoid1
- 避免多余的函数上下文绑定。 - eslint: - no-extra-bind(opens new window)- const name = function () { getName() }.bind(user) // ✗ avoid const name = function () { this.getName() }.bind(user) // ✓ ok1
 2
 3
 4
 5
 6
 7
- 避免不必要的布尔转换。 - eslint: - no-extra-boolean-cast(opens new window)- const result = true if (!!result) { // ✗ avoid // ... } const result = true if (result) { // ✓ ok // ... }1
 2
 3
 4
 5
 6
 7
 8
 9
- 不要使用多余的括号包裹函数。 - eslint: - no-extra-parens(opens new window)- const myFunc = (function () { }) // ✗ avoid const myFunc = function () { } // ✓ ok1
 2
- switch 一定要使用 break 来将条件分支正常中断。 - eslint: - no-fallthrough(opens new window)- switch (filter) { case 1: doSomething() // ✗ avoid case 2: doSomethingElse() } switch (filter) { case 1: doSomething() break // ✓ ok case 2: doSomethingElse() } switch (filter) { case 1: doSomething() // fallthrough // ✓ ok case 2: doSomethingElse() }1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
- 不要省去小数点前面的0。 - eslint: - no-floating-decimal(opens new window)- const discount = .5 // ✗ avoid const discount = 0.5 // ✓ ok1
 2
- 避免对声明过的函数重新赋值。 - eslint: - no-func-assign(opens new window)- function myFunc () { } myFunc = myOtherFunc // ✗ avoid1
 2
- 不要对全局只读对象重新赋值。 - eslint: - no-global-assign(opens new window)- window = {} // ✗ avoid1
- 注意隐式的 eval()。 - eslint: - no-implied-eval(opens new window)- setTimeout("alert('Hello world')") // ✗ avoid setTimeout(function () { alert('Hello world') }) // ✓ ok1
 2
- 嵌套的代码块中禁止再定义函数。 - eslint: - no-inner-declarations(opens new window)- if (authenticated) { function setAuthUser () {} // ✗ avoid }1
 2
 3
- 不要向 RegExp 构造器传入非法的正则表达式。 - eslint: - no-invalid-regexp(opens new window)- RegExp('[a-z') // ✗ avoid RegExp('[a-z]') // ✓ ok1
 2
- 不要使用非法的空白符。 - eslint: - no-irregular-whitespace(opens new window)- function myFunc () /*<NBSP>*/{} // ✗ avoid1
- 禁止使用 iterator。 - eslint: - no-iterator(opens new window)- Foo.prototype.__iterator__ = function () {} // ✗ avoid1
- 外部变量不要与对象属性重名。 - eslint: - no-label-var(opens new window)- var score = 100 function game () { score: while (true) { // ✗ avoid score -= 10 if (score > 0) continue score break } }1
 2
 3
 4
 5
 6
 7
 8
- 不要使用标签语句。 - eslint: - no-labels(opens new window)- label: while (true) { break label // ✗ avoid }1
 2
 3
 4
- 不要书写不必要的嵌套代码块。 - eslint: - no-lone-blocks(opens new window)- function myFunc () { { // ✗ avoid myOtherFunc() } } function myFunc () { myOtherFunc() // ✓ ok }1
 2
 3
 4
 5
 6
 7
 8
 9
- 不要混合使用空格与制表符作为缩进。 
- 除了缩进,不要使用多个空格。 - eslint: - no-multi-spaces(opens new window)- const id = 1234 // ✗ avoid const id = 1234 // ✓ ok1
 2
- 不要使用多行字符串。 - eslint: - no-multi-str(opens new window)- const message = 'Hello \ world' // ✗ avoid1
 2
- new 创建对象实例后需要赋值给变量。 - eslint: - no-new(opens new window)- new Character() // ✗ avoid const character = new Character() // ✓ ok1
 2
- 禁止使用 Function 构造器。 - eslint: - no-new-func(opens new window)- var sum = new Function('a', 'b', 'return a + b') // ✗ avoid1
- 禁止使用 Object 构造器。 - eslint: - no-new-object(opens new window)- let config = new Object() // ✗ avoid1
- 禁止使用 new require。 - eslint: - no-new-require(opens new window)- const myModule = new require('my-module') // ✗ avoid1
- 禁止使用 Symbol 构造器。 - eslint: - no-new-symbol(opens new window)- const foo = new Symbol('foo') // ✗ avoid1
- 禁止使用原始包装器。 - eslint: - no-new-wrappers(opens new window)- const message = new String('hello') // ✗ avoid1
- 不要将全局对象的属性作为函数调用。 - eslint: - no-obj-calls(opens new window)- const math = Math() // ✗ avoid1
- 不要使用八进制字面量。 - eslint: - no-octal(opens new window)- const num = 042 // ✗ avoid const num = '042' // ✓ ok1
 2
- 字符串字面量中也不要使用八进制转义字符。 - eslint: - no-octal-escape(opens new window)- const copyright = 'Copyright \251' // ✗ avoid1
- 使用 __dirname 和 __filename 时尽量避免使用字符串拼接。 - eslint: - no-path-concat(opens new window)- const pathToFile = __dirname + '/app.js' // ✗ avoid const pathToFile = path.join(__dirname, 'app.js') // ✓ ok1
 2
- 使用 - getPrototypeOf来替代 proto。- eslint: - no-proto(opens new window)- const foo = obj.__proto__ // ✗ avoid const foo = Object.getPrototypeOf(obj) // ✓ ok1
 2
- 不要重复声明变量。 - eslint: - no-redeclare(opens new window)- let name = 'John' let name = 'Jane' // ✗ avoid let name = 'John' name = 'Jane' // ✓ ok1
 2
 3
 4
 5
- 正则中避免使用多个空格。 - eslint: - no-regex-spaces(opens new window)- const regexp = /test value/ // ✗ avoid const regexp = /test {3}value/ // ✓ ok const regexp = /test value/ // ✓ ok1
 2
 3
 4
- return 语句中的赋值必需有括号包裹。 - eslint: - no-return-assign(opens new window)- function sum (a, b) { return result = a + b // ✗ avoid } function sum (a, b) { return (result = a + b) // ✓ ok }1
 2
 3
 4
 5
 6
 7
- 避免将变量赋值给自己。 - eslint: - no-self-assign(opens new window)- name = name // ✗ avoid1
- 避免将变量与自己进行比较操作。 - esint: - no-self-compare(opens new window)- if (score === score) {} // ✗ avoid1
- 避免使用逗号操作符。 - eslint: - no-sequences(opens new window)- if (doSomething(), !!test) {} // ✗ avoid1
- 不要随意更改关键字的值。 - eslint: - no-shadow-restricted-names(opens new window)- let undefined = 'value' // ✗ avoid1
- 禁止使用稀疏数组(Sparse arrays)。 - eslint: - no-sparse-arrays(opens new window)- let fruits = ['apple',, 'orange'] // ✗ avoid1
- 不要使用制表符。 - eslint: - no-tabs(opens new window)
- 正确使用 ES6 中的字符串模板。 - eslint: - no-template-curly-in-string(opens new window)- const message = 'Hello ${name}' // ✗ avoid const message = `Hello ${name}` // ✓ ok1
 2
- 使用 this 前请确保 super() 已调用。 - eslint: - no-this-before-super(opens new window)- class Dog extends Animal { constructor () { this.legs = 4 // ✗ avoid super() } }1
 2
 3
 4
 5
 6
- 用 throw 抛错时,抛出 Error 对象而不是字符串。 - eslint: - no-throw-literal(opens new window)- throw 'error' // ✗ avoid throw new Error('error') // ✓ ok1
 2
- 行末不留空格。 
- 不要使用 undefined 来初始化变量。 - eslint: - no-undef-init(opens new window)- let name = undefined // ✗ avoid let name name = 'value' // ✓ ok1
 2
 3
 4
- 循环语句中注意更新循环变量。 - eslint: - no-unmodified-loop-condition(opens new window)- for (let i = 0; i < items.length; j++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok1
 2
- 如果有更好的实现,尽量不要使用三元表达式。 - eslint: - no-unneeded-ternary(opens new window)- let score = val ? val : 0 // ✗ avoid let score = val || 0 // ✓ ok1
 2
- return,throw,continue 和 break 后不要再跟代码。 - eslint: - no-unreachable(opens new window)- function doSomething () { return true console.log('never called') // ✗ avoid }1
 2
 3
 4
- finally 代码块中不要再改变程序执行流程。 - eslint: - no-unsafe-finally(opens new window)- try { // ... } catch (e) { // ... } finally { return 42 // ✗ avoid }1
 2
 3
 4
 5
 6
 7
- 关系运算符的左值不要做取反操作。 - eslint: - no-unsafe-negation(opens new window)- if (!key in obj) {} // ✗ avoid1
- 避免不必要的 .call() 和 .apply()。 - eslint: - no-useless-call(opens new window)- sum.call(null, 1, 2, 3) // ✗ avoid1
- 避免使用不必要的计算值作对象属性。 - eslint: - no-useless-computed-key(opens new window)- const user = { ['name']: 'John Doe' } // ✗ avoid const user = { name: 'John Doe' } // ✓ ok1
 2
- 禁止多余的构造器。 - eslint: - no-useless-constructor(opens new window)- class Car { constructor () { // ✗ avoid } }1
 2
 3
 4
- 禁止不必要的转义。 - eslint: - no-useless-escape(opens new window)- let message = 'Hell\o' // ✗ avoid1
- import, export 和解构操作中,禁止赋值到同名变量。 - eslint: - no-useless-rename(opens new window)- import { config as config } from './config' // ✗ avoid import { config } from './config' // ✓ ok1
 2
- 属性前面不要加空格。 - eslint: - no-whitespace-before-property(opens new window)- user .name // ✗ avoid user.name // ✓ ok1
 2
- 禁止使用 with。 - eslint: - no-with(opens new window)- with (val) {...} // ✗ avoid1
- 对象属性换行时注意统一代码风格。 - eslint: - object-property-newline(opens new window)- const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' // ✗ avoid } const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
- 代码块中避免多余留白。 - eslint: - padded-blocks(opens new window)- if (user) { // ✗ avoid const name = getName() } if (user) { const name = getName() // ✓ ok }1
 2
 3
 4
 5
 6
 7
 8
 9
- 展开运算符与它的表达式间不要留空白。 - eslint: - rest-spread-spacing(opens new window)- fn(... args) // ✗ avoid fn(...args) // ✓ ok1
 2
- 遇到分号时空格要后留前不留。 - eslint: - semi-spacing(opens new window)- for (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok1
 2
- 代码块首尾留空格。 - eslint: - space-before-blocks(opens new window)- if (admin){...} // ✗ avoid if (admin) {...} // ✓ ok1
 2
- 圆括号间不留空格。 - eslint: - space-in-parens(opens new window)- getName( name ) // ✗ avoid getName(name) // ✓ ok1
 2
- 一元运算符后面跟一个空格。 - eslint: - space-unary-ops(opens new window)- typeof!admin // ✗ avoid typeof !admin // ✓ ok1
 2
- 注释首尾留空格。 - eslint: - spaced-comment(opens new window)- //comment // ✗ avoid // comment // ✓ ok /*comment*/ // ✗ avoid /* comment */ // ✓ ok1
 2
 3
 4
 5
- 模板字符串中变量前后不加空格。 - eslint: - template-curly-spacing(opens new window)- const message = `Hello, ${ name }` // ✗ avoid const message = `Hello, ${name}` // ✓ ok1
 2
- 检查 NaN 的正确姿势是使用 isNaN()。 - eslint: - use-isnan(opens new window)- if (price === NaN) { } // ✗ avoid if (isNaN(price)) { } // ✓ ok1
 2
- 用合法的字符串跟 typeof 进行比较操作。 - eslint: - valid-typeof(opens new window)- typeof name === 'undefimed' // ✗ avoid typeof name === 'undefined' // ✓ ok1
 2
- 自调用匿名函数 (IIFEs) 使用括号包裹。 - eslint: - wrap-iife(opens new window)- const getName = function () { }() // ✗ avoid const getName = (function () { }()) // ✓ ok const getName = (function () { })() // ✓ ok1
 2
 3
 4
- yield * 中的 * 前后都要有空格。 - eslint: - yield-star-spacing(opens new window)- yield* increment() // ✗ avoid yield * increment() // ✓ ok1
 2
- 请书写优雅的条件语句(avoid Yoda conditions)。 - eslint: - yoda(opens new window)- if (42 === age) { } // ✗ avoid if (age === 42) { } // ✓ ok1
 2
# 关于分号
- 不要使用分号。 (参见:1 (opens new window),2 (opens new window),3 (opens new window)) - eslint: - semi(opens new window)- window.alert('hi') // ✓ ok window.alert('hi'); // ✗ avoid1
 2
- 不要使用 - (,- [, or ``` 等作为一行的开始。在没有分号的情况下代码压缩后会导致报错,而坚持这一规范则可避免出错。- eslint: - no-unexpected-multiline(opens new window)- // ✓ ok ;(function () { window.alert('ok') }()) // ✗ avoid (function () { window.alert('ok') }())1
 2
 3
 4
 5
 6
 7
 8
 9- // ✓ ok ;[1, 2, 3].forEach(bar) // ✗ avoid [1, 2, 3].forEach(bar)1
 2
 3
 4
 5- // ✓ ok ;`hello`.indexOf('o') // ✗ avoid `hello`.indexOf('o')1
 2
 3
 4
 5- 备注:上面的写法只能说聪明过头了。 - 相比更加可读易懂的代码,那些看似投巧的写法是不可取的。 - 譬如: - ;[1, 2, 3].forEach(bar)1- 建议的写法是: - var nums = [1, 2, 3] nums.forEach(bar)1
 2
