Javascript 编码规范
编写 Javascript 代码时,我们需要遵循一定的规范,以保证代码的可读性、可维护性和一致性。本文将介绍一些常见的 Javascript 编码规范,帮助大家编写出高质量的 Javascript 代码。
本套代码规范大量参考了《 JavaScript Standard Style(JavaScript 标准编码风格)》和《Airbnb JavaScript 编码规范(涵盖 ECMAScript 6+)》。以《JavaScript Standard Style》为基础,补充《Airbnb JavaScript 编码规范(涵盖 ECMAScript 6+)》部分规范和一些补充。
细则
1. 缩进(空格与制表符)
使用两个空格进行缩进
eslint:
indentjsfunction hello (name) { console.log('hi', name) }不要使用制表符。
eslint:
no-tabs不要混合使用空格与制表符作为缩进。
eslint:
no-mixed-spaces-and-tabs
2. 字符串 String
除需要转义的情况外,字符串统一使用单引号。
eslint:
quotesjsconsole.log('hello there') var double = "a string containing 'single' quotes"字符串拼接操作符 (Infix operators) 之间要留空格。
eslint:
space-infix-opsjs// ✓ ok var x = 2 var message = 'hello, ' + name + '!'js// ✗ avoid var x=2 var message = 'hello, '+name+'!'不要使用多行字符串。
eslint:
no-multi-strjsconst message = 'Hello \ world' // ✗ avoid字符串字面量中也不要使用八进制转义字符。
eslint:
no-octal-escapejsconst copyright = 'Copyright \251' // ✗ avoid正确使用 ES6 中的字符串模板。
eslint:
no-template-curly-in-stringjsconst message = 'Hello ${name}' // ✗ avoid const message = `Hello ${name}` // ✓ ok模板字符串中变量前后不加空格。
eslint:
template-curly-spacingjavascriptconst message = `Hello, ${ name }` // ✗ avoid const message = `Hello, ${name}` // ✓ ok禁止不必要的转义。
eslint: [`no-useless-escape`](http://eslint.org/docs/rules/no-useless-escape) ```js let message = 'Hell\o' // ✗ avoid ```
3. 数字 Number
不要使用八进制字面量。
eslint:
no-octaljsconst octal = 042 // ✗ avoid const decimal = 34 // ✓ ok const octalString = '042' // ✓ ok检查
NaN的正确姿势是使用isNaN()。eslint:
use-isnanjavascriptif (price === NaN) { } // ✗ avoid if (isNaN(price)) { } // ✓ ok不要省去小数点前面的0。
eslint:
no-floating-decimaljsconst discount = .5 // ✗ avoid const discount = 0.5 // ✓ ok
4. 布尔 Boolean
避免不必要的布尔转换。
eslint:
no-extra-boolean-castjsconst result = true if (!!result) { // ✗ avoid // ... } const result = true if (result) { // ✓ ok // ... }
5. Symbol
禁止使用
Symbol构造器。eslint:
no-new-symboljsconst foo = new Symbol('foo') // ✗ avoid
6. 对象 Object
禁止使用
Object构造器。eslint:
no-new-objectjslet config = new Object() // ✗ avoid对象属性换行时注意统一代码风格。
eslint:
object-property-newlinejsconst user = { name: 'Jane Doe', age: 30, username: 'jdoe86' // ✗ avoidjs } const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok const user = { name: 'Jane Doe', age: 30, username: 'jdoe86' } // ✓ ok使用
getPrototypeOf来替代__proto__。eslint:
no-protojsconst foo = obj.__proto__ // ✗ avoid const foo = Object.getPrototypeOf(obj) // ✓ ok不要扩展原生对象。
eslint:
no-extend-nativejsObject.prototype.age = 21 // ✗ avoid对象中定义了存值器,一定要对应的定义取值器。
eslint:
accessor-pairsjsvar person = { set name (value) { // ✗ avoid this._name = value } } var person = { set name (value) { this._name = value }, get name () { // ✓ ok return this._name } }不要将全局对象的属性作为函数调用。
eslint:
no-obj-callsjsconst math = Math() // ✗ avoid对象字面量中不要定义重复的属性。
eslint:
no-dupe-keysjsvar user = { name: 'Jane Doe', name: 'John Doe' // ✗ avoid }不要对全局只读对象重新赋值。
eslint:
no-global-assignjswindow = {} // ✗ avoid避免使用不必要的计算值作对象属性。
eslint:
no-useless-computed-keyjsconst user = { ['name']: 'John Doe' } // ✗ avoid const user = { name: 'John Doe' } // ✓ ok点号操作符须与属性需在同一行。
eslint:
dot-locationjsconsole. log('hello') // ✗ avoid console .log('hello') // ✓ ok不要解构空值。
eslint:
no-empty-patternjsconst { a: {} } = foo // ✗ avoid const { a: { b } } = foo // ✓ ok使用对象属性速记语法。
eslint:
object-shorthandjsconst lukeSkywalker = 'Luke Skywalker'; // bad const obj = { lukeSkywalker: lukeSkywalker, }; // good const obj = { lukeSkywalker, };
7. 数组 Array
使用数组字面量而不是构造器。
eslint:
no-array-constructorjsvar nums = new Array(1, 2, 3) // ✗ avoid var nums = [1, 2, 3] // ✓ ok禁止使用稀疏数组(Sparse arrays)。
eslint:
no-sparse-arraysjslet fruits = ['apple',, 'orange'] // ✗ avoid
8. 变量 Variables
不要定义未使用的变量
eslint:
no-unused-varsjsfunction myFunction () { var result = something() // ✗ avoid }使用浏览器全局变量时加上
window.前缀。document、console和navigator除外。eslint:
no-undefjswindow.alert('hi') // ✓ ok总是使用
const或let来声明变量。 不这样做会导致产生全局变量。 我们希望避免污染全局命名空间。(补充)eslint:
no-undefprefer-constJavaScript// bad superPower = new SuperPower(); // good const superPower = new SuperPower();每个 var 关键字单独声明一个变量。
eslint:
one-varjs// ✓ ok var silent = true var verbose = true // ✗ avoid var silent = true, verbose = true // ✗ avoid var silent = true, verbose = true避免修改使用
const声明的变量。eslint:
no-const-assignjsconst score = 100 score = 125 // ✗ avoid不要对变量使用
delete操作。eslint:
no-delete-varjsvar name delete name // ✗ avoid不要重复声明变量。
eslint:
no-redeclarejslet name = 'John' let name = 'Jane' // ✗ avoid let name = 'John' name = 'Jane' // ✓ ok避免将变量赋值给自己。
eslint:
no-self-assignjsname = name // ✗ avoid避免将变量与自己进行比较操作。
esint:
no-self-comparejsif (score === score) {} // ✗ avoid不要使用
undefined来初始化变量。eslint:
no-undef-initjslet name = undefined // ✗ avoid let name name = 'value' // ✓ ok
9. 函数 Function
函数声明时括号与函数名间加空格。
eslint:
space-before-function-parenjsfunction name (arg) { ... } // ✓ ok function name(arg) { ... } // ✗ avoid run(function () { ... }) // ✓ ok run(function() { ... }) // ✗ avoid函数调用时标识符与括号间不留间隔。
eslint:
func-call-spacingjsconsole.log ('hello') // ✗ avoid console.log('hello') // ✓ ok不要定义冗余的函数参数。
eslint:
no-dupe-argsjsfunction sum (a, b, a) { // ✗ avoid // ... } function sum (a, b, c) { // ✓ ok // ... }不要使用多余的括号包裹函数。
eslint:
no-extra-parensjsconst myFunc = (function () { }) // ✗ avoid const myFunc = function () { } // ✓ ok避免对声明过的函数重新赋值。
eslint:
no-func-assignjsfunction myFunc () { } myFunc = myOtherFunc // ✗ avoid自调用匿名函数 (IIFEs) 使用括号包裹。
eslint:
wrap-iifejavascriptconst getName = function () { }() // ✗ avoid const getName = (function () { }()) // ✓ ok const getName = (function () { })() // ✓ ok避免多余的函数上下文绑定。
eslint:
no-extra-bindjsconst name = function () { getName() }.bind(user) // ✗ avoid const name = function () { this.getName() }.bind(user) // ✓ ok嵌套的代码块中禁止再定义函数。
eslint:
no-inner-declarationsjsif (authenticated) { function setAuthUser () {} // ✗ avoid }避免使用
arguments.callee和arguments.caller。eslint:
no-callerjsfunction foo (n) { if (n <= 0) return arguments.callee(n - 1) // ✗ avoid } function foo (n) { if (n <= 0) return foo(n - 1) }return 语句中的赋值必需有括号包裹。
eslint:
no-return-assignjsfunction sum (a, b) { return result = a + b // ✗ avoid } function sum (a, b) { return (result = a + b) // ✓ ok }禁止使用
Function构造器。eslint:
no-new-funcjsvar sum = new Function('a', 'b', 'return a + b') // ✗ avoid不要书写不必要的嵌套代码块。
eslint:
no-lone-blocksjsfunction myFunc () { { // ✗ avoid myOtherFunc() } } function myFunc () { myOtherFunc() // ✓ ok }
10. 类 Classes & 构造函数 Constructors
总是使用
class。避免直接操作prototype。(补充)为什么? 因为
class语法更为简洁更易读。js// bad function Queue(contents = []) { this.queue = [...contents]; } Queue.prototype.pop = function () { const value = this.queue[0]; this.queue.splice(0, 1); return value; }; // good class Queue { constructor(contents = []) { this.queue = [...contents]; } pop() { const value = this.queue[0]; this.queue.splice(0, 1); return value; } }使用
extends继承。(补充) 为什么?因为
extends是一个内置的原型继承方法并且不会破坏instanceof。js// bad const inherits = require('inherits'); function PeekableQueue(contents) { Queue.apply(this, contents); } inherits(PeekableQueue, Queue); PeekableQueue.prototype.peek = function () { return this.queue[0]; }; // good class PeekableQueue extends Queue { peek() { return this.queue[0]; } }如果没有指定,类有一个默认的构造函数。一个空的构造函数或者只是委托给父类则不是必须的。
eslint:
no-useless-constructorjs// bad class Jedi { constructor() {} getName() { return this.name; } } // bad class Rey extends Jedi { constructor(...args) { super(...args); } } // good class Rey extends Jedi { constructor(...args) { super(...args); this.name = 'Rey'; } }类中不要定义冗余的属性。
eslint:
no-dupe-class-membersjsclass Dog { bark () {} bark () {} // ✗ avoid }构造函数要以大写字母开头。
eslint:
new-capjsfunction animal () {} var dog = new animal() // ✗ avoid function Animal () {} var dog = new Animal() // ✓ ok无参的构造函数调用时要带上括号。
eslint:
new-parensjsfunction Animal () {} var dog = new Animal // ✗ avoid var dog = new Animal() // ✓ ok子类的构造器中一定要调用
supereslint:
constructor-superjsclass Dog { constructor () { super() // ✗ avoid } } class Dog extends Mammal { constructor () { super() // ✓ ok } }避免对类名重新赋值。
eslint:
no-class-assignjsclass Dog {} Dog = 'Fido' // ✗ avoidnew创建对象实例后需要赋值给变量。eslint: [`no-new`](http://eslint.org/docs/rules/no-new) ```js new Character() // ✗ avoid const character = new Character() // ✓ ok ```使用
this前请确保super()已调用。eslint: [`no-this-before-super`](http://eslint.org/docs/rules/no-this-before-super) ```js class Dog extends Animal { constructor () { this.legs = 4 // ✗ avoid super() } } ```避免不必要的
.call()和.apply()。eslint: [`no-useless-call`](http://eslint.org/docs/rules/no-useless-call) ```js sum.call(null, 1, 2, 3) // ✗ avoid ```
11. 空格的添加
关键字后面加空格。
eslint:
keyword-spacingjsif (condition) { ... } // ✓ ok if(condition) { ... } // ✗ avoid单行代码块两边加空格。
eslint:
block-spacingjsfunction foo () {return true} // ✗ avoid function foo () { return true } // ✓ ok键值对当中冒号与值之间要留空白。
jsvar obj = { 'key' : 'value' } // ✗ avoid var obj = { 'key' :'value' } // ✗ avoid var obj = { 'key':'value' } // ✗ avoid var obj = { 'key': 'value' } // ✓ ok除了缩进,不要使用多个空格。
eslint:
no-multi-spacesjsconst id = 1234 // ✗ avoid const id = 1234 // ✓ ok展开运算符与它的表达式间不要留空白。
eslint:
rest-spread-spacingjsfn(... args) // ✗ avoid fn(...args) // ✓ ok遇到分号时空格要后留前不留。
eslint:
semi-spacingjsfor (let i = 0 ;i < items.length ;i++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ ok圆括号间不留空格。
eslint:
space-in-parensjsgetName( name ) // ✗ avoid getName(name) // ✓ ok代码块首尾留空格。
eslint:
space-before-blocksjsif (admin){...} // ✗ avoid if (admin) {...} // ✓ ok一元运算符后面跟一个空格。
eslint: [`space-unary-ops`](http://eslint.org/docs/rules/space-unary-ops) ```javascript typeof!admin // ✗ avoid typeof !admin // ✓ ok ```行末不留空格。
eslint: [`no-trailing-spaces`](http://eslint.org/docs/rules/no-trailing-spaces)
12. 结构体(代码块)
使用大括号包裹所有的多行代码块。
eslint:
nonblock-statement-body-position条件判断结构体
多行 if 语句的的括号不能省。
eslint:
curlyjs// ✓ ok if (options.quiet !== true) console.log('done')js// ✓ ok if (options.quiet !== true) { console.log('done') }js// ✗ avoid if (options.quiet !== true) console.log('done')else 关键字要与花括号保持在同一行。
eslint:
brace-stylejs// ✓ ok if (condition) { // ... } else { // ... }js// ✗ avoid if (condition) { // ... } else { // ... }始终使用
===替代==。例外:
obj == null可以用来检查null || undefined。eslint:
eqeqeqjsif (name === 'John') // ✓ ok if (name == 'John') // ✗ avoidjsif (name !== 'John') // ✓ ok if (name != 'John') // ✗ avoid条件语句中赋值语句使用括号包起来。这样使得代码更加清晰可读,而不会认为是将条件判断语句的全等号(
===)错写成了等号(=)。eslint:
no-cond-assignjs// ✓ ok while ((m = text.match(expr))) { // ... } // ✗ avoid while (m = text.match(expr)) { // ... }关系运算符的左值不要做取反操作。
eslint:
no-unsafe-negationjsif (!key in obj) {} // ✗ avoid请书写优雅的条件语句(avoid Yoda conditions)。
eslint:
yodajavascriptif (42 === age) { } // ✗ avoid if (age === 42) { } // ✓ ok
循环结构体
循环语句中注意更新循环变量。
eslint:
no-unmodified-loop-conditionjsfor (let i = 0; i < items.length; j++) {...} // ✗ avoid for (let i = 0; i < items.length; i++) {...} // ✓ okswitch一定要使用break来将条件分支正常中断。eslint:
no-fallthroughjsswitch (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() } ``switch语句中不要定义重复的case分支。eslint:
no-duplicate-casejsswitch (id) { case 1: // ... case 1: // ✗ avoid }
三元运算符
对于三元运算符
?和:与他们所负责的代码处于同一行eslint:
operator-linebreakjs// ✓ 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'如果有更好的实现,尽量不要使用三元表达式。
eslint:
no-unneeded-ternaryjslet score = val ? val : 0 // ✗ avoid let score = val || 0 // ✓ ok
不要使用标签语句。
eslint:
no-labelsjslabel: while (true) { break label // ✗ avoid }禁止使用
with。eslint:
no-withjswith (val) {...} // ✗ avoid避免使用常量作为条件表达式的条件(循环语句除外)。
eslint:
no-constant-conditionjsif (false) { // ✗ avoid // ... } if (x === 0) { // ✓ ok // ... } while (true) { // ✓ ok // ... }try...catch结构catch中不要对错误重新赋值。eslint:
no-ex-assignjstry { // ... } catch (e) { e = 'new value' // ✗ avoid } try { // ... } catch (e) { const newVal = 'new value' // ✓ ok }不要丢掉异常处理中
err参数。eslint:
handle-callback-errjs// ✓ ok run(function (err) { if (err) throw err window.alert('done') })js// ✗ avoid run(function (err) { window.alert('done') })用
throw抛错时,抛出Error对象而不是字符串。eslint:
no-throw-literaljsthrow 'error' // ✗ avoid throw new Error('error') // ✓ okfinally代码块中不要再改变程序执行流程。eslint:
no-unsafe-finallyjstry { // ... } catch (e) { // ... } finally { return 42 // ✗ avoid }
13. 正则表达式
正则中不要使用控制符。
eslint:
no-control-regexjsvar pattern = /\x1f/ // ✗ avoid var pattern = /\x20/ // ✓ ok正则中不要使用空字符。
eslint:
no-empty-character-classjsconst myRegex = /^abc[]/ // ✗ avoid const myRegex = /^abc[a-z]/ // ✓ ok正则中避免使用多个空格。
eslint:
no-regex-spacesjsconst regexp = /test value/ // ✗ avoid const regexp = /test {3}value/ // ✓ ok const regexp = /test value/ // ✓ ok不要向
RegExp构造器传入非法的正则表达式。eslint:
no-invalid-regexpjsRegExp('[a-z') // ✗ avoid RegExp('[a-z]') // ✓ ok
14. 模块
同一模块有多个导入时一次性写完。
eslint:
no-duplicate-importsjsimport { myFunc1 } from 'module' import { myFunc2 } from 'module' // ✗ avoid import { myFunc1, myFunc2 } from 'module' // ✓ ok禁止使用
new require。eslint:
no-new-requirejsconst myModule = new require('my-module') // ✗ avoidimport, export 和解构操作中,禁止赋值到同名变量。
eslint:
no-useless-renamejsimport { config as config } from './config' // ✗ avoid import { config } from './config' // ✓ ok将所有
import导入放在非导入语句的上面。eslint:
import/first由于
import被提升,保持他们在顶部,防止意外的行为。
15. 逗号
逗号后面加空格。
eslint:
comma-spacingjs// ✓ ok var list = [1, 2, 3, 4] function greet (name, options) { ... }js// ✗ avoid var list = [1,2,3,4] function greet (name,options) { ... }始终将逗号置于行末。
eslint:
comma-stylejsvar obj = { foo: 'foo' ,bar: 'bar' // ✗ avoid } var obj = { foo: 'foo', bar: 'bar' // ✓ ok }不允许有多余的行末逗号。
eslint:
comma-danglejsvar obj = { message: 'hello', // ✗ avoid }避免使用逗号操作符。
eslint:
no-sequencesjsif (doSomething(), !!test) {} // ✗ avoid
16. 命名规则
命名要有意义,避免用单字符变量和少见单词来命名。尽量做到通过命名就可以大概知道其作用是什么。
变量、属性和函数名统一采用小驼峰;
eslint:
camelcasejsfunction my_function () { } // ✗ avoid function myFunction () { } // ✓ ok var my_var = 'hello' // ✗ avoid var myVar = 'hello' // ✓ ok类名、构造函数统一采用大驼峰;
eslint:
new-cap使用大写标识常量;
如果代码引入Jquery,Jquery变量命名以
$开头;
17. 注释
注释首尾留空格。
eslint:
spaced-commentjavascript//comment // ✗ avoid // comment // ✓ ok /*comment*/ // ✗ avoid /* comment */ // ✓ ok**单行注释使用
//**。将单行注释放在续注释的语句上方。在注释之前放置一个空行,除非它位于代码块的第一行。js// ✗ avoid const active = true; // is current tab // ✓ ok // is current tab const active = true;多行注释使用
/** ... */。js/** * 删除图片 * @param {string} 要删除的文件名 * @retuen {boolean} 删除的结果 */ async delUploadImage(filename) { let res = await this.$HttpApi.delUploadImage(filename); let flat = false; if (res.status === 200) { console.log(`删除图片:${this.formItem.imgSrc}`); flat = true; } else { console.log(`删除图片失败`); flat = false; } return flat; },function的注释function 没有参数,使用多行注释;
function 携带参数,在描述下面添加变量说明以及回传说明
js/** * close 事件 */ handleClose() { // 内容省略 }, /** * 删除图片 * @param {string} 要删除的文件名 * @retuen {boolean} 删除的结果 */ async delUploadImage(filename) { let res = await this.$HttpApi.delUploadImage(filename); let flat = false; if (res.status === 200) { console.log(`删除图片:${this.formItem.imgSrc}`); flat = true; } else { console.log(`删除图片失败`); flat = false; } return flat; },
变量的注释
变量要尽量保证明了易懂,可以让人快速了解其作用。原则上,变量的说明从变量命名就开始了。也就是,变量的作用最好可以从变量的命名中直接可以得出。如果变量的命名不能很好的体现变量的作用,这时就需要通过注释进行说明了。
18. 一些禁用
禁止使用 __iterator__
eslint: no-iterator
Foo.prototype.__iterator__ = function () {} // ✗ avoid建议使用高阶函数:
使用
map()/every()/filter()/find()/findIndex()/reduce()/some()/ … 来迭代数组, 使用Object.keys()/Object.values()/Object.entries()来生成数组,以便可以迭代对象。
各种遍历方法的优缺点
for(;;){}- 描述:该方法是最原始的遍历方法;
- 优点:直接明了;
- 缺点:结构比while循环复杂,容易出编码错误;只能遍历数组;
forEach- 描述:接收三个参数,第一个是value,第二个是index,第三个是数组体;用于调用数组的每个元素,并将元素传递给回调函数;
- 优点:便利的时候更加简洁,效率和for循环相同,不用关心集合下标的问题,减少了出错的效率;
- 缺点:不能同时遍历多个集合;在遍历的时候无法修改和删除集合数据;方法不能使用break,continue语句跳出循环,或者使用return从函数体返回;对于空数组不会执行回调函数;不能遍历对象;
for...in...描述:用于循环遍历数组或对象的可枚举属性,遍历的是对象(数组也是对象的一种)的key值;
优点:可以遍历数组的键名,遍历对象简洁方便(常用与对象的遍历)
缺点:某些情况下,会出现随机顺序的遍历,因为里面的key值是string类型,所以增加了转换过程,因此开销较大;因为对象的可枚举属性包括自有属性、继承自原型的属性,所以可能会遍历出意料之外的属性;
如果需要遍历对象的继承属性,
for...in...反而是较优的选择。
for...of...- 描述:它是ES6中新增加的语法,用来循环获取一对键值对中的value。因为数组也是对象的一种,所以也适用于数组的遍历
- 优点:避免了for in的所有缺点,可以使用break,continue和return;不仅支持数组的遍历,还可以遍历类似数组的对象,支持字符串的遍历;最简洁,最直接的遍历数组的语法;支持map和Set对象遍历;
- 缺点:不适用于处理原有的原生对象(原生对象是一个子集,包含一些在运动过程中动态创建的对象);
不要使用
debugger。eslint:
no-debuggerjsfunction sum (a, b) { debugger // ✗ avoid return a + b }不要使用非法的空白符。
eslint:
no-irregular-whitespacejsfunction myFunc () /*<NBSP>*/{} // ✗ avoidreturn,throw,continue和break后不要再跟代码。eslint:
no-unreachablejsfunction doSomething () { return true console.log('never called') // ✗ avoid }不允许有连续多行空行。
eslint:
no-multiple-empty-linesjs// ✓ ok var value = 'hello world' console.log(value)js// ✗ avoid var value = 'hello world' console.log(value)不要使用
eval()。eslint:
no-evaljseval( "var result = user." + propName ) // ✗ avoid var result = user[propName] // ✓ ok注意隐式的
eval()。eslint:
no-implied-evaljssetTimeout("alert('Hello world')") // ✗ avoid setTimeout(function () { alert('Hello world') }) // ✓ ok不要随意更改关键字的值。
eslint:
no-shadow-restricted-namesjslet undefined = 'value' // ✗ avoid代码块中避免多余留白。
eslint:
padded-blocksjsif (user) { // ✗ avoid const name = getName() } if (user) { const name = getName() // ✓ ok }使用
__dirname和__filename时尽量避免使用字符串拼接。eslint:
no-path-concatjsconst pathToFile = __dirname + '/app.js' // ✗ avoid const pathToFile = path.join(__dirname, 'app.js') // ✓ ok禁止使用原始包装器。
eslint:
no-new-wrappersjsconst message = new String('hello') // ✗ avoid var numberObject = new Number(33) // ✗ avoid var booleanObject = new Boolean(false) // ✗ avoid
19. 一些必要的要求
文件末尾留一空行。
eslint:
eol-lastyield \*中的\*前后都要有空格。eslint:
yield-star-spacingjavascriptyield* increment() // ✗ avoid yield * increment() // ✓ ok用合法的字符串跟
typeof进行比较操作。eslint:
valid-typeofjavascripttypeof name === 'undefimed' // ✗ avoid typeof name === 'undefined' // ✓ ok
20. 关于分号
eslint:
semijswindow.alert('hi') // ✓ ok window.alert('hi'); // ✗ avoid不要使用
(,[, or ``` 等作为一行的开始。在没有分号的情况下代码压缩后会导致报错,而坚持这一规范则可避免出错。eslint:
no-unexpected-multilinejs// ✓ ok ;(function () { window.alert('ok') }()) // ✗ avoid (function () { window.alert('ok') }())js// ✓ ok ;[1, 2, 3].forEach(bar) // ✗ avoid [1, 2, 3].forEach(bar)js// ✓ ok ;`hello`.indexOf('o') // ✗ avoid `hello`.indexOf('o')备注:上面的写法只能说聪明过头了。
相比更加可读易懂的代码,那些看似投巧的写法是不可取的。
譬如:
;[1, 2, 3].forEach(bar)建议的写法是:
var nums = [1, 2, 3] nums.forEach(bar)
