Note: this guide assumes you are using Babel, and requires that you use babel-preset-airbnb or the equivalent. It also assumes you are installing shims/polyfills in your app, with airbnb-browser-shims or the equivalent.
This guide is available in other languages too. See Translation
Why? This ensures that you can’t reassign your references, which can lead to bugs and difficult to comprehend code.
// badvara=1;varb=2;// goodconsta=1;constb=2;
2.2 If you must reassign references, use let instead of var. eslint: no-var
Why? let is block-scoped rather than function-scoped like var.
// badvarcount=1;if(true){count+=1;}// good, use the let.letcount=1;if(true){count+=1;}
2.3 Note that both let and const are block-scoped, whereas var is function-scoped.
// const and let only exist in the blocks they are defined in.{leta=1;constb=1;varc=1;}console.log(a);// ReferenceErrorconsole.log(b);// ReferenceErrorconsole.log(c);// Prints 1
In the above code, you can see that referencing a and b will produce a ReferenceError, while c contains the number. This is because a and b are block scoped, while c is scoped to the containing function.
3.7 Do not call Object.prototype methods directly, such as hasOwnProperty, propertyIsEnumerable, and isPrototypeOf. eslint: no-prototype-builtins
Why? These methods may be shadowed by properties on the object in question - consider { hasOwnProperty: false } - or, the object may be a null object (Object.create(null)).
// badconsole.log(object.hasOwnProperty(key));// goodconsole.log(Object.prototype.hasOwnProperty.call(object,key));// bestconsthas=Object.prototype.hasOwnProperty;// cache the lookup once, in module scope.console.log(has.call(object,key));/* or */importhasfrom'has';// https://www.npmjs.com/package/hasconsole.log(has(object,key));
3.8 Prefer the object spread syntax over Object.assign to shallow-copy objects. Use the object rest parameter syntax to get a new object with certain properties omitted. eslint: prefer-object-spread
4.7 Use return statements in array method callbacks. It’s ok to omit the return if the function body consists of a single statement returning an expression without side effects, following 8.2. eslint: array-callback-return
// good[1,2,3].map((x)=>{consty=x+1;returnx*y;});// good[1,2,3].map((x)=>x+1);// bad - no returned value means `acc` becomes undefined after the first iteration[[0,1],[2,3],[4,5]].reduce((acc,item,index)=>{constflatten=acc.concat(item);});// good[[0,1],[2,3],[4,5]].reduce((acc,item,index)=>{constflatten=acc.concat(item);returnflatten;});// badinbox.filter((msg)=>{const{ subject, author }=msg;if(subject==='Mockingbird'){returnauthor==='Harper Lee';}else{returnfalse;}});// goodinbox.filter((msg)=>{const{ subject, author }=msg;if(subject==='Mockingbird'){returnauthor==='Harper Lee';}returnfalse;});
4.8 Use line breaks after open and before close array brackets if an array has multiple lines
5.1 Use object destructuring when accessing and using multiple properties of an object. eslint: prefer-destructuring
Why? Destructuring saves you from creating temporary references for those properties, and from repetitive access of the object. Repeating object access creates more repetitive code, requires more reading, and creates more opportunities for mistakes. Destructuring objects also provides a single site of definition of the object structure that is used in the block, rather than requiring reading the entire block to determine what is used.
请发表评论