JavaScript Standard Style(代码风格)
JavaScript Standard Style
翻译: Português, Spanish, 繁體中文, 简体中文
standard 规则列表,太多不必阅读。
了解 standard 的最好方式是安装它,然后写代码尝试。
规则
-
缩进使用两个空格。
eslint:
indentfunction hello (name) { console.log('hi', name) } -
字符串使用单引号,除非是为了避免转义。
eslint:
quotesconsole.log('hello there') $("<div class='box'>") -
无未使用的变量。
eslint:
no-unused-varsfunction myFunction () { var result = something() // ✗ avoid } -
关键字后面要有一个空格。
eslint:
keyword-spacingif (condition) { ... } // ✓ ok if(condition) { ... } // ✗ avoid -
函数参数列表括号前面要有一个空格。
eslint:
space-before-function-parenfunction name (arg) { ... } // ✓ ok function name(arg) { ... } // ✗ avoid run(function () { ... }) // ✓ ok run(function() { ... }) // ✗ avoid -
始终使用
===不使用==。
例外:可以使用obj == null检测null || undefined。eslint:
eqeqeqif (name === 'John') // ✓ ok if (name == 'John') // ✗ avoidif (name !== 'John') // ✓ ok if (name != 'John') // ✗ avoid -
中缀操作符(infix operators)前后要有一个空格。
eslint:
space-infix-ops// ✓ ok var x = 2 var message = 'hello, ' + name + '!'// ✗ avoid var x=2 var message = 'hello, '+name+'!' -
逗号后面有一个空格。
eslint:
comma-spacing// ✓ ok var list = [1, 2, 3, 4] function greet (name, options) { ... }// ✗ avoid var list = [1,2,3,4] function greet (name,options) { ... } -
else与它的大括号同行。eslint:
brace-style// ✓ ok if (condition) { // ... } else { // ... }// ✗ avoid if (condition) { // ... } else { // ... } -
if语句如果包含多个语句则使用大括号。eslint:
curly// ✓ ok if (options.quiet !== true) console.log('done')// ✓ ok if (options.quiet !== true) { console.log('done') }// ✗ avoid if (options.quiet !== true) console.log('done') -
始终处理函数的
err参数。eslint:
handle-callback-err// ✓ ok run(function (err) { if (err) throw err window.alert('done') })// ✗ avoid run(function (err) { window.alert('done') }) -
浏览器全局变量始终添加前缀
window.。
例外:document,console和navigator。eslint:
no-undefwindow.alert('hi') // ✓ ok -
不要有多个连续空行。
eslint:
no-multiple-empty-lines// ✓ ok var value = 'hello world' console.log(value)// ✗ avoid var value = 'hello world' console.log(value) -
三元表达式如果是多行,则
?和:放在各自的行上。eslint:
operator-linebreak// ✓ 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' -
var声明,每个声明占一行。eslint:
one-var// ✓ ok var silent = true var verbose = true // ✗ avoid var silent = true, verbose = true // ✗ avoid var silent = true, verbose = true -
用括号包裹条件中的赋值表达式。这是为了清楚的表明它是一个赋值表达式 (
=),而不是一个等式 (===) 的误写。eslint:
no-cond-assign// ✓ ok while ((m = text.match(expr))) { // ... } // ✗ avoid while (m = text.match(expr)) { // ... } -
单行语句块的内侧要有空格。
eslint:
block-spacingfunction foo () {return true} // ✗ avoid function foo () { return true } // ✓ ok -
变量和函数的名字使用 camelCase 格式。
eslint:
camelcasefunction my_function () { } // ✗ avoid function myFunction () { } // ✓ ok var my_var = 'hello' // ✗ avoid var myVar = 'hello' // ✓ ok -
无多余逗号。
eslint:
comma-danglevar obj = { message: 'hello', // ✗ avoid } -
逗号必须放在当前行的末尾。
eslint:
comma-stylevar obj = { foo: 'foo' ,bar: 'bar' // ✗ avoid } var obj = { foo: 'foo', bar: 'bar' // ✓ ok } -
.应当与属性同行。eslint:
dot-locationconsole. log('hello') // ✗ avoid console .log('hello') // ✓ ok -
文件以空行结尾。
elint:
eol-last -
函数名字和调用括号之间没有空格。
eslint:
func-call-spacingconsole.log ('hello') // ✗ avoid console.log('hello') // ✓ ok -
键名和键值之间要有空格。
eslint:
key-spacingvar obj = { 'key' : 'value' } // ✗ avoid var obj = { 'key' :'value' } // ✗ avoid var obj = { 'key':'value' } // ✗ avoid var obj = { 'key': 'value' } // ✓ ok -
构造函数的名字以大写字母开始。
eslint:
new-capfunction animal () {} var dog = new animal() // ✗ avoid function Animal () {} var dog = new Animal() // ✓ ok -
没有参数的构造函数在调用时必须有括号。
eslint:
new-parensfunction Animal () {} var dog = new Animal // ✗ avoid var dog = new Animal() // ✓ ok -
对象若定义了 setter 则必须定义相应的 getter。
eslint:
accessor-pairsvar person = { set name (value) { // ✗ avoid this.name = value } } var person = { set name (value) { this.name = value }, get name () { // ✓ ok return this.name } } -
子类的构造器必须调用
super。eslint:
constructor-superclass Dog { constructor () { super() // ✗ avoid } } class Dog extends Mammal { constructor () { super() // ✓ ok } } -
使用对象字面量,不使用对象构造函数。
eslint:
no-array-constructorvar nums = new Array(1, 2, 3) // ✗ avoid var nums = [1, 2, 3] // ✓ ok -
不使用
arguments.callee和arguments.caller。eslint:
no-callerfunction foo (n) { if (n <= 0) return arguments.callee(n - 1) // ✗ avoid } function foo (n) { if (n <= 0) return foo(n - 1) } -
不要给 class 赋值。
eslint:
no-class-assignclass Dog {} Dog = 'Fido' // ✗ avoid -
不要修改由
const声明的变量。eslint:
no-const-assignconst score = 100 score = 125 // ✗ avoid -
在条件句中不要使用常量,循环语句除外。
eslint:
no-constant-conditionif (false) { // ✗ avoid // ... } if (x === 0) { // ✓ ok // ... } while (true) { // ✓ ok // ... } -
正则表达式不要使用控制字符。
eslint:
no-control-regexvar pattern = /\x1f/ // ✗ avoid var pattern = /\x20/ // ✓ ok -
不使用
debugger语句。eslint:
no-debuggerfunction sum (a, b) { debugger // ✗ avoid return a + b } -
不要对变量使用
delete操作符。eslint:
no-delete-varvar name delete name // ✗ avoid -
函数定义无重复参数。
eslint:
no-dupe-argsfunction sum (a, b, a) { // ✗ avoid // ... } function sum (a, b, c) { // ✓ ok // ... } -
class 定义无重复成员。
eslint:
no-dupe-class-membersclass Dog { bark () {} bark () {} // ✗ avoid } -
对象字面量无重复键名。
eslint:
no-dupe-keysvar user = { name: 'Jane Doe', name: 'John Doe' // ✗ avoid } -
switch语句无重复case从句。eslint:
no-duplicate-caseswitch (id) { case 1: // ... case 1: // ✗ avoid } -
每个模块只使用一个 import 语句。
eslint:
no-duplicate-importsimport { myFunc1 } from 'module' import { myFunc2 } from 'module' // ✗ avoid import { myFunc1, myFunc2 } from 'module' // ✓ ok -
正则表达式无空的字符组。
eslint:
no-empty-character-classconst myRegex = /^abc[]/ // ✗ avoid const myRegex = /^abc[a-z]/ // ✓ ok -
解构赋值不使用空的 pattern。
eslint:
no-empty-patternconst { a: {} } = foo // ✗ avoid const { a: { b } } = foo // ✓ ok -
不使用
eval()。eslint:
no-evaleval( "var result = user." + propName ) // ✗ avoid var result = user[propName] // ✓ ok -
catch语句中不要对错误对象重新赋值。eslint:
no-ex-assigntry { // ... } catch (e) { e = 'new value' // ✗ avoid } try { // ... } catch (e) { const newVal = 'new value' // ✓ ok } -
不要扩展原生对象。
eslint:
no-extend-nativeObject.prototype.age = 21 // ✗ avoid -
不使用非必要的
.bind()。eslint:
no-extra-bindconst name = function () { getName() }.bind(user) // ✗ avoid const name = function () { this.getName() }.bind(user) // ✓ ok -
不使用非必要的布尔值转换。
eslint:
no-extra-boolean-castconst result = true if (!!result) { // ✗ avoid // ... } const result = true if (result) { // ✓ ok // ... } -
函数表达式不使用非必要的包裹括号。
eslint:
no-extra-parensconst myFunc = (function () { }) // ✗ avoid const myFunc = function () { } // ✓ ok -
switch语句使用break,避免运行到下一个case。eslint:
no-fallthroughswitch (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() } -
浮点数应包含整数和小数。
eslint:
no-floating-decimalconst discount = .5 // ✗ avoid const discount = 0.5 // ✓ ok -
不给声明过的函数重新赋值。
eslint:
no-func-assignfunction myFunc () { } myFunc = myOtherFunc // ✗ avoid -
不给只读的全局变量重新赋值。
eslint:
no-global-assignwindow = {} // ✗ avoid -
不使用隐式
eval()。eslint:
no-implied-evalsetTimeout("alert('Hello world')") // ✗ avoid setTimeout(function () { alert('Hello world') }) // ✓ ok -
不在嵌套语句中使用函数声明。
eslint:
no-inner-declarationsif (authenticated) { function setAuthUser () {} // ✗ avoid } -
RegExp构造器不使用非法的正则表达式字符串。eslint:
no-invalid-regexpRegExp('[a-z') // ✗ avoid RegExp('[a-z]') // ✓ ok -
不使用非法空白。
eslint:
no-irregular-whitespacefunction myFunc () /*<NBSP>*/{} // ✗ avoid -
不使用
__iterator__。eslint:
no-iteratorFoo.prototype.__iterator__ = function () {} // ✗ avoid -
label 不使用作用域内变量的名字。
eslint:
no-label-varvar score = 100 function game () { score: 50 // ✗ avoid } -
不使用 label 语句。
eslint:
no-labelslabel: while (true) { break label // ✗ avoid } -
不使用非必要的嵌套语句块。
eslint:
no-lone-blocksfunction myFunc () { { // ✗ avoid myOtherFunc() } } function myFunc () { myOtherFunc() // ✓ ok } -
缩进不混用空格和制表符。
eslint:
no-mixed-spaces-and-tabs -
不使用多个连续空格,缩进除外。
eslint:
no-multi-spacesconst id = 1234 // ✗ avoid const id = 1234 // ✓ ok -
不使用多行字符串。
eslint:
no-multi-strconst message = 'Hello \ world' // ✗ avoid -
如果不是赋值则不使用
new。eslint:
no-newnew Character() // ✗ avoid const character = new Character() // ✓ ok -
不使用
Function构造器。eslint:
no-new-funcvar sum = new Function('a', 'b', 'return a + b') // ✗ avoid -
不使用
Object构造器。eslint:
no-new-objectlet config = new Object() // ✗ avoid -
不使用
new require。eslint:
no-new-requireconst myModule = new require('my-module') // ✗ avoid -
不使用
Symbol构造器。eslint:
no-new-symbolconst foo = new Symbol('foo') // ✗ avoid -
不使用原始类型的包装对象。
eslint:
no-new-wrappersconst message = new String('hello') // ✗ avoid -
全局对象的属性不用于函数调用。
eslint:
no-obj-callsconst math = Math() // ✗ avoid -
不使用八进制字面量。
eslint:
no-octalconst num = 042 // ✗ avoid const num = '042' // ✓ ok -
字符串不使用八进制转义。
eslint:
no-octal-escapeconst copyright = 'Copyright \251' // ✗ avoid -
__dirname和__filename不用于字符串拼接。eslint:
no-path-concatconst pathToFile = __dirname + '/app.js' // ✗ avoid const pathToFile = path.join(__dirname, 'app.js') // ✓ ok -
不使用
__proto__,应使用getPrototypeOf。eslint:
no-protoconst foo = obj.__proto__ // ✗ avoid const foo = Object.getPrototypeOf(obj) // ✓ ok -
不重复声明变量。
eslint:
no-redeclarelet name = 'John' let name = 'Jane' // ✗ avoid let name = 'John' name = 'Jane' // ✓ ok -
正则表达式中不使用多个连续空白。
eslint:
no-regex-spacesconst regexp = /test value/ // ✗ avoid const regexp = /test {3}value/ // ✓ ok const regexp = /test value/ // ✓ ok -
在 return 语句中赋值表达式要用括号包裹。
eslint:
no-return-assignfunction sum (a, b) { return result = a + b // ✗ avoid } function sum (a, b) { return (result = a + b) // ✓ ok } -
不将变量赋值给它自身。
eslint:
no-self-assignname = name // ✗ avoid -
不将变量跟它自身相比。
esint:
no-self-compareif (score === score) {} // ✗ avoid -
不使用逗号操作符。
eslint:
no-sequencesif (doSomething(), !!test) {} // ✗ avoid -
不修改关键字的值。
eslint:
no-shadow-restricted-nameslet undefined = 'value' // ✗ avoid -
不使用稀疏数组(Sparse arrays)。
eslint:
no-sparse-arrayslet fruits = ['apple',, 'orange'] // ✗ avoid -
不使用制表符。
eslint:
no-tabs -
普通字符串不要包含模板字符串占位符。
eslint:
no-template-curly-in-stringconst message = 'Hello ${name}' // ✗ avoid const message = `Hello ${name}` // ✓ ok -
super()必须在访问this之前调用。eslint:
no-this-before-superclass Dog extends Animal { constructor () { this.legs = 4 // ✗ avoid super() } } -
throw应当抛出一个Error对象。eslint:
no-throw-literalthrow 'error' // ✗ avoid throw new Error('error') // ✓ ok -
行末不要有空白。
eslint:
no-trailing-spaces -
变量不初始化为
undefined。eslint:
no-undef-initlet name = undefined // ✗ avoid let name name = 'value' // ✓ ok -
循环语句要更新循环变量。
eslint:
no-unmodified-loop-conditionfor (let i = 0; i < items.length; j++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok -
简单的存在赋值不使用三元操作符。
eslint:
no-unneeded-ternarylet score = val ? val : 0 // ✗ avoid let score = val || 0 // ✓ ok -
return,throw,continue,break语句后面不要有代码。eslint:
no-unreachablefunction doSomething () { return true console.log('never called') // ✗ avoid } -
finally语句块无流程控制语句。eslint:
no-unsafe-finallytry { // ... } catch (e) { // ... } finally { return 42 // ✗ avoid } -
in操作符的左操作数不要使用!。eslint:
no-unsafe-negationif (!key in obj) {} // ✗ avoid -
无非必要的
.call()和.apply()。eslint:
no-useless-callsum.call(null, 1, 2, 3) // ✗ avoid -
无非必要的计算属性。
eslint:
no-useless-computed-keyconst user = { ['name']: 'John Doe' } // ✗ avoid const user = { name: 'John Doe' } // ✓ ok -
无非必要的构造器。
eslint:
no-useless-constructorclass Car { constructor () { // ✗ avoid } } -
无非必要的转义。
eslint:
no-useless-escapelet message = 'Hell\o' // ✗ avoid -
import, export, 解构赋值不可重命名为同名变量。
eslint:
no-useless-renameimport { config as config } from './config' // ✗ avoid import { config } from './config' // ✓ ok -
属性前面无空白。
eslint:
no-whitespace-before-propertyuser .name // ✗ avoid user.name // ✓ ok12 -
不使用
with语句。eslint:
no-withwith (val) {...} // ✗ avoid -
对象属性的换行应一致。
eslint:
object-property-newlineconst 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' } // ✓ ok -
语句块内部首尾无空行。
eslint:
padded-blocksif (user) { // ✗ avoid const name = getName() } if (user) { const name = getName() // ✓ ok } -
展开操作符后面无空格。
eslint:
rest-spread-spacingfn(... args) // ✗ avoid fn(...args) // ✓ ok -
分号后面要有一个空格,前面无空格。
eslint:
semi-spacingfor (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok -
语句块前面要有一个空格。
eslint:
space-before-blocksif (admin){...} // ✗ avoid if (admin) {...} // ✓ ok -
函数参数列表括号内侧无空格。
eslint:
space-in-parensgetName( name ) // ✗ avoid getName(name) // ✓ ok -
一元操作符后面要有一个空格。
eslint:
space-unary-opstypeof!admin // ✗ avoid typeof !admin // ✓ ok -
注释符号后面要有空白。
eslint:
spaced-comment//comment // ✗ avoid // comment // ✓ ok /*comment*/ // ✗ avoid /* comment */ // ✓ ok -
模板字符串大括号内侧无空格。
eslint:
template-curly-spacingconst message = `Hello, ${ name }` // ✗ avoid const message = `Hello, ${name}` // ✓ ok -
使用
isNaN()检查NaN。eslint:
use-isnanif (price === NaN) { } // ✗ avoid if (isNaN(price)) { } // ✓ ok -
typeof必须跟合法的字符串比较。eslint:
valid-typeoftypeof name === 'undefimed' // ✗ avoid typeof name === 'undefined' // ✓ ok -
立即调用函数 (IIFEs) 必须用括号包裹。
eslint:
wrap-iifeconst getName = function () { }() // ✗ avoid const getName = (function () { }()) // ✓ ok const getName = (function () { })() // ✓ ok -
yield*的*前后要有一个空格。eslint:
yield-star-spacingyield* increment() // ✗ avoid yield * increment() // ✓ ok -
不使用 Yoda 式条件句比较。
eslint:
yodaif (42 === age) { } // ✗ avoid if (age === 42) { } // ✓ ok
分号
-
eslint:
semiwindow.alert('hi') // ✓ ok window.alert('hi'); // ✗ avoid -
不以
(,[, “` 开始行。这是省略分号时唯一的陷阱。standard 会保护你不落入陷阱。eslint:
no-unexpected-multiline// ✓ ok ;(function () { window.alert('ok') }()) // ✗ avoid (function () { window.alert('ok') }())// ✓ ok ;[1, 2, 3].forEach(bar) // ✗ avoid [1, 2, 3].forEach(bar)// ✓ ok ;`hello`.indexOf('o') // ✗ avoid `hello`.indexOf('o')提示:如果你经常这样写代码,你可能是过于聪明了。
不鼓励过于聪明的简写,表达式应尽可能清晰且容易阅读:
不要这样:
;[1, 2, 3].forEach(bar)这样更好:
var nums = [1, 2, 3] nums.forEach(bar)
拓展阅读
- An Open Letter to JavaScript Leaders Regarding Semicolons
- JavaScript Semicolon Insertion – Everything you need to know
一个有用的视频
现在所有流行的代码压缩器都是通过 AST 压缩,因此它们在处理没有分号的 JavaScript 代码时没有问题(因为 JavaScript 不是必须使用分号)。
开始引用 “An Open Letter to JavaScript Leaders Regarding Semicolons”
[依赖自动插入分号机制]的代码是非常安全的,是完全合法的 JavaScript 代码,各浏览器都能正确解析;Closure compiler、yuicompressor、packer 及 jsmin 都能正确压缩。没有任何性能影响。
抱歉,我不是向你说教,这个语言的社区领导者在撒谎,并且害怕告诉你真相。真是羞耻。我建议,先了解 JavaScript 语句是如何结束的以及什么情况不会结束,之后你可以写出漂亮的代码。
一般来说,\n 结束语句,除非:
- 语句没有关闭括号、数组字面量、对象字面量,或者以其它不合法的方式结束,比如以
.或,结束。 - 当前行是
--或++,这时它将递减或递增下一个 token。 - 当前行是
for(),while(),do,if(), 或else,并且没有<span class="p">{</span>。 - 下一行行首是
[,(,+,*,/,-,,,.,或者是二进制操作符——它们只能出现在一个表达式的两个操作数之间。
第一条显而易见。像这些情况:JSON 或括号内有 \n 字符;一个 var 多行声明,每行以 , 结束,即使是 JSLint 都没问题。
第二条很怪。我从没有看到这种写法 i\n++\nj。事实上,它被解析为 i; ++j,而不是 i++; j。
第三条很好理解。if (x)\ny() 等于 if (x) { y() }。这个语句直到遇到一个语句块或语句才结束。
; 是一个合法的 JavaScript 语句,所以 if(x); 等于 if(x){} 或 “If x, do nothing.” 。这更多用于循环,这时循环测试同时也是更新函数。不常见,但不是没听过。
第四条通常是那些因循守旧的人提到的情况:“不,你需要分号!”。但是,事实证明,如果你的意思是这些行不是上一行的连续行,那么在这些行之前加上分号非常容易。例如
foo();
[1,2,3].forEach(bar);
可以这么写:
foo()
;[1,2,3].forEach(bar)
这么做的好处是,一旦你习惯了以 ( 或 [ 开始的行没有分号,你会很容易注意到行首的分号。
结束引用 “An Open Letter to JavaScript Leaders Regarding Semicolons”

浙公网安备 33010602011771号