本文基於 github 項目 airbnb/javascript 翻譯,也加入了一些我的理解。規範有利於咱們更好的提升代碼可讀性,避免一些沒必要要的 bug。可是,並無統一的標準和硬性要求,這裏只是給你們提供一些參考,適合團隊和本身的纔是最好的。javascript
我的博客地址 🍹🍰 fe-codecss
基本類型賦值時,應該直接使用類型的值html
string
number
boolean
null
undefined
symbol
const foo = 1;
let bar = foo;
bar = 9;
console.log(foo, bar); // => 1,9
複製代碼
複雜類型賦值實際上是地址的引用前端
object
array
function
const foo = [1, 2];
const bar = foo;
bar[0] = 9;
console.log(foo[0], bar[0]); // => 9, 9
// const 只能阻止引用類型地址的從新賦值
// 並不能保證引用類型的屬性等不變
複製代碼
const
,避免使用var
. eslint: prefer-const
, no-const-assign
儘可能確保你的代碼中的狀態是可控範圍內的,重複引用會出現難以理解的 bug 和代碼。vue
// bad
var a = 1;
var b = 2;
// good
const a = 1;
const b = 2;
複製代碼
let
,而不是var
. eslint: no-var
let
是塊級做用域,var
是函數級做用域,一樣是爲了減小代碼的不可控,減小 「意外」java
// bad
var count = 1;
if (true) {
count += 1;
}
// good, use the let.
let count = 1;
if (true) {
count += 1;
}
複製代碼
let
、const
都是塊級做用域// const 和 let 都只存在於它定義的那個塊級做用域
{
let a = 1;
const b = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError
複製代碼
no-new-object
// bad
const item = new Object();
// good
const item = {};
複製代碼
function getKey(k) {
return `a key named ${k}`;
}
// bad
const obj = {
id: 5,
name: 'San Francisco',
};
obj[getKey('enabled')] = true;
// good getKey('enabled')是動態屬性名
const obj = {
id: 5,
name: 'San Francisco',
[getKey('enabled')]: true,
};
複製代碼
object-shorthand
// bad
const atom = {
value: 1,
addValue: function (value) {
return atom.value + value;
},
};
// good
const atom = {
value: 1,
// 對象的方法
addValue(value) {
return atom.value + value;
},
};
複製代碼
object-shorthand
const lukeSkywalker = 'Luke Skywalker';
// bad
const obj = {
lukeSkywalker: lukeSkywalker,
};
// good
const obj = {
lukeSkywalker
};
複製代碼
const anakinSkywalker = 'Anakin Skywalker';
const lukeSkywalker = 'Luke Skywalker';
// bad
const obj = {
episodeOne: 1,
twoJediWalkIntoACantina: 2,
lukeSkywalker,
episodeThree: 3,
mayTheFourth: 4,
anakinSkywalker,
};
// good
const obj = {
lukeSkywalker,
anakinSkywalker,
episodeOne: 1,
twoJediWalkIntoACantina: 2,
episodeThree: 3,
mayTheFourth: 4,
};
複製代碼
''
. eslint: quote-props
通常來講,咱們認爲它在主觀上更容易閱讀。它改進了語法突出顯示,而且更容易被JS引擎優化。node
// bad
const bad = {
'foo': 3,
'bar': 4,
'data-blah': 5,
};
// good
const good = {
foo: 3,
bar: 4,
'data-blah': 5,
};
複製代碼
Object.prototype
上的方法,如hasOwnProperty
, propertyIsEnumerable
, isPrototypeOf
。在一些有問題的對象上, 這些方法可能會被屏蔽掉 - 如:
{ hasOwnProperty: false }
- 或這是一個空對象Object.create(null)
webpack
// bad
console.log(object.hasOwnProperty(key));
// good
console.log(Object.prototype.hasOwnProperty.call(object, key));
// best
const has = Object.prototype.hasOwnProperty; // 在模塊做用內作一次緩存
/* or */
import has from 'has'; // https://www.npmjs.com/package/has
// ...
console.log(has.call(object, key));
複製代碼
...
,而不是Object.assign
。解構賦值獲取對象指定的幾個屬性時,推薦用 rest 運算符,也是 ...
。// very bad
const original = { a: 1, b: 2 };
const copy = Object.assign(original, { c: 3 });
delete copy.a; // so does this 改變了 original
// bad
const original = { a: 1, b: 2 };
const copy = Object.assign({}, original, { c: 3 }); // copy => { a: 1, b: 2, c: 3 }
// good
const original = { a: 1, b: 2 };
const copy = { ...original, c: 3 }; // copy => { a: 1, b: 2, c: 3 }
const { a, ...noA } = copy; // noA => { b: 2, c: 3 }
複製代碼
no-array-constructor
// bad
const items = new Array();
// good
const items = [];
複製代碼
const someStack = [];
// bad
someStack[someStack.length] = 'abracadabra';
// good
someStack.push('abracadabra');
複製代碼
// bad
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i += 1) {
itemsCopy[i] = items[i];
}
// good
const itemsCopy = [...items];
複製代碼
...
運算符而不是Array.from
來將一個類數組轉換成數組。const foo = document.querySelectorAll('.foo');
// good
const nodes = Array.from(foo);
// best
const nodes = [...foo];
複製代碼
Array.from
去將一個類數組對象轉成一個數組。const arrLike = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
// bad
const arr = Array.prototype.slice.call(arrLike);
// good
const arr = Array.from(arrLike);
複製代碼
Array.from
而不是 ...
運算符去迭代。 這樣能夠避免建立一箇中間數組。// bad
const baz = [...foo].map(bar);
// good
const baz = Array.from(foo, bar);
複製代碼
array-callback-return
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
// good 函數只有一個語句
[1, 2, 3].map(x => x + 1);
// bad 沒有返回值, 致使在第一次迭代後acc 就變成undefined了
[[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
const flatten = acc.concat(item);
acc[index] = flatten;
});
// good
[[0, 1], [2, 3], [4, 5]].reduce((acc, item, index) => {
const flatten = acc.concat(item);
acc[index] = flatten;
return flatten;
});
// bad
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird') {
return author === 'Harper Lee';
} else {
return false;
}
});
// good
inbox.filter((msg) => {
const { subject, author } = msg;
if (subject === 'Mockingbird') {
return author === 'Harper Lee';
}
return false;
});
複製代碼
[
後和 ]
前換行。// bad
const arr = [
[0, 1], [2, 3], [4, 5],
];
const objectInArray = [{
id: 1,
}, {
id: 2,
}];
const numberInArray = [
1, 2,
];
// good
const arr = [[0, 1], [2, 3], [4, 5]];
const objectInArray = [
{
id: 1,
},
{
id: 2,
},
];
const numberInArray = [
1,
2,
];
複製代碼
prefer-destructuring
這樣就不須要給這些屬性建立臨時/引用git
// bad
function getFullName(user) {
const firstName = user.firstName;
const lastName = user.lastName;
return `${firstName} ${lastName}`;
}
// good
function getFullName(user) {
const { firstName, lastName } = user;
return `${firstName} ${lastName}`;
}
// best
function getFullName({ firstName, lastName }) {
return `${firstName} ${lastName}`;
}
複製代碼
const arr = [1, 2, 3, 4];
// bad
const first = arr[0];
const second = arr[1];
// good
const [first, second] = arr;
複製代碼
不依賴於返回值的順序,更可讀es6
// bad
function processInput(input) {
// 而後就是見證奇蹟的時刻
return [left, right, top, bottom];
}
const [left, __, top] = processInput(input);
// good
function processInput(input) {
return { left, right, top, bottom };
}
const { left, top } = processInput(input);
複製代碼
''
。 eslint: quotes
// bad
const name = "Capt. Janeway";
// bad - 模板應該包含插入文字或換行
const name = `Capt. Janeway`;
// good
const name = 'Capt. Janeway';
複製代碼
+
鏈接換行字符串。很差用,且可讀性差
// bad
const errorMessage = 'This is a super long error that was thrown because \ of Batman. When you stop to think about how Batman had anything to do \ with this, you would get nowhere \ fast.';
// bad
const errorMessage = 'This is a super long error that was thrown because ' +
'of Batman. When you stop to think about how Batman had anything to do ' +
'with this, you would get nowhere fast.';
// good
const errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.';
複製代碼
+
來拼接字符串。 eslint: prefer-template
template-curly-spacing
模板字符串更具可讀性、語法簡潔、字符串插入參數。
// bad
function sayHi(name) {
return 'How are you, ' + name + '?';
}
// bad
function sayHi(name) {
return ['How are you, ', name, '?'].join();
}
// bad
function sayHi(name) {
return `How are you, ${ name }?`;
}
// good
function sayHi(name) {
return `How are you, ${name}?`;
}
複製代碼
eval()
,漏洞太多。 eslint: no-eval
no-useless-escape
反斜線可讀性差,只在必要時使用
// bad
const foo = '\'this\' \i\s \"quoted\"';
// good
const foo = '\'this\' is "quoted"';
//best
const foo = `my name is '${name}'`;
複製代碼
func-style
函數聲明做用域會提高,下降了代碼可讀性和可維護性。若是你發現一個函數又大又複雜,這個函數妨礙這個文件其餘部分的理解性,這可能就是時候把這個函數單獨抽成一個模塊了。(Discussion)
// bad
function foo() {
// ...
}
// bad
const foo = function () {
// ...
};
// good
const short = function longUniqueMoreDescriptiveLexicalFoo() {
// ...
};
複製代碼
wrap-iife
一個當即調用的函數表達式是一個單元 - 把它和他的調用者(圓括號)包裹起來。固然,現代模塊開發中,你基本用不到。
// immediately-invoked function expression (IIFE)
(function () {
console.log('Welcome to the Internet. Please follow me.');
}());
複製代碼
7.3 不要在非函數塊(if、while等等)內聲明函數。而是把這個函數分配給一個變量。瀏覽器會容許你這樣作,但瀏覽器解析方式不一樣,結果也許會有差別。【詳見no-loop-func
】 eslint: no-loop-func
7.4 注意: 在ECMA-262中 [塊 block
] 的定義是: 一系列的語句; 可是函數聲明不是一個語句。 函數表達式是一個語句。
// bad
if (currentUser) {
function test() {
console.log('Nope.');
}
}
// good
let test;
if (currentUser) {
test = () => {
console.log('Yup.');
};
}
複製代碼
arguments
命名參數。它的優先級高於每一個函數做用域自帶的 arguments
對象, 因此會致使函數自帶的 arguments
值被覆蓋。// bad
function foo(name, options, arguments) {
// ...
}
// good
function foo(name, options, args) {
// ...
}
複製代碼
...
,而不是 arguments
。 eslint: prefer-rest-params
...
更明確你想用哪些參數。
// bad
function concatenateAll() {
const args = Array.prototype.slice.call(arguments);
return args.join('');
}
// good
function concatenateAll(...args) {
return args.join('');
}
複製代碼
// really bad
function handleThings(opts) {
// 雖然你想這麼寫, 可是這個會帶來一些細微的bug
// 若是 opts 的值爲 false, 它會被賦值爲 {}
opts = opts || {};
// ...
}
// still bad
function handleThings(opts) {
if (opts === void 0) {
opts = {};
}
// ...
}
// good
function handleThings(opts = {}) {
// ...
}
複製代碼
var b = 1;
// bad
function count(a = b++) {
console.log(a);
}
count(); // 1
count(); // 2
count(3); // 3
count(); // 3
// 很容易讓人懵逼
複製代碼
// bad
function handleThings(opts = {}, name) {
// ...
}
// good
function handleThings(name, opts = {}) {
// ...
}
複製代碼
no-new-func
// bad
var add = new Function('a', 'b', 'return a + b');
// still bad
var subtract = Function('a', 'b', 'return a - b');
複製代碼
space-before-function-paren
space-before-blocks
// bad
const f = function(){};
const g = function (){};
const h = function() {};
// good
const x = function () {};
const y = function a() {};
複製代碼
no-param-reassign
特別注意引用類型的操做,保證數據的不可變性
// bad
function f1(obj) {
obj.key = 1;
};
// good
function f2(obj) {
const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;
};
複製代碼
no-param-reassign
// bad
function f1(a) {
a = 1;
// ...
}
function f2(a) {
if (!a) { a = 1; }
// ...
}
// good
function f3(a) {
const b = a || 1;
// ...
}
function f4(a = 1) {
// ...
}
複製代碼
...
。 eslint: prefer-spread
Why? 這樣更清晰,你沒必要提供上下文,並且你不能輕易地用
apply
來組成new
// bad
const x = [1, 2, 3, 4, 5];
console.log.apply(console, x);
// good
const x = [1, 2, 3, 4, 5];
console.log(...x);
// bad
new (Function.prototype.bind.apply(Date, [null, 2016, 8, 5]));
// good
new Date(...[2016, 8, 5]);
複製代碼
// bad
function foo(bar, baz, quux) {
// ...
}
// good
function foo( bar, baz, quux, ) {
// ...
}
// bad
console.log(foo,
bar,
baz);
// good
console.log(
foo,
bar,
baz,
);
複製代碼
prefer-arrow-callback
, arrow-spacing
它建立了一個在上下文中執行的函數,這一般是您想要的,而且是一種更簡潔的語法。
// bad
[1, 2, 3].map(function (x) {
const y = x + 1;
return x * y;
});
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
複製代碼
return
語句。 eslint: arrow-parens
, arrow-body-style
// bad
[1, 2, 3].map(number => {
const nextNumber = number + 1;
`A string containing the ${nextNumber}.`;
});
// good
[1, 2, 3].map(number => `A string containing the ${number}.`);
// good
[1, 2, 3].map((number) => {
const nextNumber = number + 1;
return `A string containing the ${nextNumber}.`;
});
// good
[1, 2, 3].map((number, index) => ({
[index]: number
}));
// 表達式有反作用就不要用隱式返回
function foo(callback) {
const val = callback();
if (val === true) {
// Do something if callback returns true
}
}
let bool = false;
// bad
foo(() => bool = true);
// good
foo(() => {
bool = true;
});
複製代碼
// bad
['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
httpMagicObjectWithAVeryLongName,
httpMethod
)
);
// good
['get', 'post', 'put'].map(httpMethod => (
Object.prototype.hasOwnProperty.call(
httpMagicObjectWithAVeryLongName,
httpMethod
)
));
複製代碼
arrow-parens
// bad
[1, 2, 3].map((x) => x * x);
// good
[1, 2, 3].map(x => x * x);
// good
[1, 2, 3].map(number => (
`A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`
));
// bad
[1, 2, 3].map(x => {
const y = x + 1;
return x * y;
});
// good
[1, 2, 3].map((x) => {
const y = x + 1;
return x * y;
});
複製代碼
=>
和比較操做符 <=, >=
混淆. eslint: no-confusing-arrow
// bad
const itemHeight = item => item.height > 256 ? item.largeSize : item.smallSize;
// bad
const itemHeight = (item) => item.height > 256 ? item.largeSize : item.smallSize;
// good
const itemHeight = item => (item.height > 256 ? item.largeSize : item.smallSize);
// good
const itemHeight = (item) => {
const { height, largeSize, smallSize } = item;
return height > 256 ? largeSize : smallSize;
};
複製代碼
implicit-arrow-linebreak
// bad
(foo) =>
bar;
(foo) =>
(bar);
// good
(foo) => bar;
(foo) => (bar);
(foo) => (
bar
)
複製代碼
class
,避免直接操做prototype
// 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
實現繼承內置的方法來繼承原型,而不會破壞
instanceof
// 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];
}
}
複製代碼
this
來實現方法鏈// bad
Jedi.prototype.jump = function () {
this.jumping = true;
return true;
};
Jedi.prototype.setHeight = function (height) {
this.height = height;
};
const luke = new Jedi();
luke.jump(); // => true
luke.setHeight(20); // => undefined
// good
class Jedi {
jump() {
this.jumping = true;
return this;
}
setHeight(height) {
this.height = height;
return this;
}
}
const luke = new Jedi();
luke.jump()
.setHeight(20);
複製代碼
class Jedi {
constructor(options = {}) {
this.name = options.name || 'no name';
}
getName() {
return this.name;
}
toString() {
return `Jedi - ${this.getName()}`;
}
}
複製代碼
no-useless-constructor
// 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';
}
}
複製代碼
no-dupe-class-members
重複類成員會默默的執行最後一個,有重複確定就是一個錯誤
// bad
class Foo {
bar() { return 1; }
bar() { return 2; }
}
// good
class Foo {
bar() { return 1; }
}
// good
class Foo {
bar() { return 2; }
}
複製代碼
import
/export
)。或者隨時換成其餘的首選模塊系統。// bad
const AirbnbStyleGuide = require('./AirbnbStyleGuide');
module.exports = AirbnbStyleGuide.es6;
// ok
import AirbnbStyleGuide from './AirbnbStyleGuide';
export default AirbnbStyleGuide.es6;
// best
import { es6 } from './AirbnbStyleGuide';
export default es6;
複製代碼
// bad
import * as AirbnbStyleGuide from './AirbnbStyleGuide';
// good
import AirbnbStyleGuide from './AirbnbStyleGuide';
複製代碼
看起來簡潔,可是影響可讀性
// bad
// filename es6.js
export { es6 as default } from './AirbnbStyleGuide';
// good
// filename es6.js
import { es6 } from './AirbnbStyleGuide';
export default es6;
複製代碼
no-duplicate-imports
Why? 從同一個路徑下import多行會使代碼難以維護
// bad
import foo from 'foo';
// … some other imports … //
import { named1, named2 } from 'foo';
// good
import foo, { named1, named2 } from 'foo';
// good
import foo, {
named1,
named2,
} from 'foo';
複製代碼
import/no-mutable-exports
儘可能減小狀態,保證數據的不可變性。雖然在某些場景下可能須要這種技術,但總的來講應該導出常量。
// bad
let foo = 3;
export { foo }
// good
const foo = 3;
export { foo }
複製代碼
export default
更好。 eslint: import/prefer-default-export
鼓勵使用更多文件,每一個文件只作一件事情並導出,這樣可讀性和可維護性更好。
// bad
export function foo() {}
// good
export default function foo() {}
複製代碼
import
放在其餘全部語句以前。 eslint: import/first
防止意外行爲。
// bad
import foo from 'foo';
foo.init();
import bar from 'bar';
// good
import foo from 'foo';
import bar from 'bar';
foo.init();
複製代碼
// bad
import {longNameA, longNameB, longNameC, longNameD, longNameE} from 'path';
// good
import {
longNameA,
longNameB,
longNameC,
longNameD,
longNameE,
} from 'path';
複製代碼
import/no-webpack-loader-syntax
最好是在
webpack.config.js
裏寫
// bad
import fooSass from 'css!sass!foo.scss';
import barCss from 'style!css!bar.css';
// good
import fooSass from 'foo.scss';
import barCss from 'bar.css';
複製代碼
for-in
、 for-of
。 eslint: no-iterator
no-restricted-syntax
不可變原則,處理純函數的返回值比處理反作用更容易。
數組的迭代方法:
map()
/every()
/filter()
/find()
/findIndex()
/reduce()
/some()
/ ... , 對象的處理方法 :Object.keys()
/Object.values()
/Object.entries()
去產生一個數組, 這樣你就能去遍歷對象了。
const numbers = [1, 2, 3, 4, 5];
// bad
let sum = 0;
for (let num of numbers) {
sum += num;
}
sum === 15;
// good
let sum = 0;
numbers.forEach(num => sum += num);
sum === 15;
// best (use the functional force)
const sum = numbers.reduce((total, num) => total + num, 0);
sum === 15;
// bad
const increasedByOne = [];
for (let i = 0; i < numbers.length; i++) {
increasedByOne.push(numbers[i] + 1);
}
// good
const increasedByOne = [];
numbers.forEach(num => increasedByOne.push(num + 1));
// best (keeping it functional)
const increasedByOne = numbers.map(num => num + 1);
複製代碼
兼容性很差
generator-star-spacing
function
和*
是同一律念,關鍵字*
不是function
的修飾符,function*
是一個和function
不同的獨特結構
// bad
function * foo() {
// ...
}
// bad
const bar = function * () {
// ...
}
// bad
const baz = function *() {
// ...
}
// bad
const quux = function*() {
// ...
}
// bad
function*foo() {
// ...
}
// bad
function *foo() {
// ...
}
// very bad
function * foo() {
// ...
}
// very bad
const wat = function * () {
// ...
}
// good
function* foo() {
// ...
}
// good
const foo = function* () {
// ...
}
複製代碼
dot-notation
const luke = {
jedi: true,
age: 28,
};
// bad
const isJedi = luke['jedi'];
// good
const isJedi = luke.jedi;
複製代碼
[]
const luke = {
jedi: true,
age: 28,
};
function getProp(prop) {
return luke[prop];
}
const isJedi = getProp('jedi');
複製代碼
**
。 eslint: no-restricted-properties
.// bad
const binary = Math.pow(2, 10);
// good
const binary = 2 ** 10;
複製代碼
const
或 let
聲明變量。若是你不想遇到一對變量提高、全局變量的 bug 的話。 eslint: no-undef
prefer-const
// bad
superPower = new SuperPower();
// good
const superPower = new SuperPower();
複製代碼
const
或 let
。 eslint: one-var
// bad
const items = getItems(),
goSportsTeam = true,
dragonball = 'z';
// bad
// (compare to above, and try to spot the mistake)
const items = getItems(),
goSportsTeam = true;
dragonball = 'z';
// good
const items = getItems();
const goSportsTeam = true;
const dragonball = 'z';
複製代碼
const
放一塊兒,let
放一塊兒新變量依賴以前的變量或常量時,是有幫助的
// bad
let i, len, dragonball,
items = getItems(),
goSportsTeam = true;
// bad
let i;
const items = getItems();
let dragonball;
const goSportsTeam = true;
let len;
// good
const goSportsTeam = true;
const items = getItems();
let dragonball;
let i;
let length;
複製代碼
// bad - unnecessary function call
function checkName(hasName) {
const name = getName();
if (hasName === 'test') {
return false;
}
if (name === 'test') {
this.setName('');
return false;
}
return name;
}
// good
function checkName(hasName) {
if (hasName === 'test') {
return false;
}
// 在須要的時候分配
const name = getName();
if (name === 'test') {
this.setName('');
return false;
}
return name;
}
複製代碼
no-multi-assign
Why? 連接變量分配建立隱式全局變量。
// bad
(function example() {
// JavaScript 將其解釋爲
// let a = ( b = ( c = 1 ) );
// let 只對變量 a 起做用; 變量 b 和 c 都變成了全局變量
let a = b = c = 1;
}());
console.log(a); // undefined
console.log(b); // 1
console.log(c); // 1
// good
(function example() {
let a = 1;
let b = a;
let c = a;
}());
console.log(a); // undefined
console.log(b); // undefined
console.log(c); // undefined
// `const` 也同樣
複製代碼
++
, --
). eslint no-plusplus
根據 eslint 文檔,一元遞增和遞減語句受到自動分號插入的影響,而且可能會致使應用程序中的值遞增或遞減的靜默錯誤。 使用num += 1 而不是 num++ 或代替語句來改變你的值也更具表現力。禁止一元遞增和遞減語句也會阻止您無心中預先遞增/預遞減值,從而減小程序出現意外行爲。
// bad
let array = [1, 2, 3];
let num = 1;
num++;
--num;
let sum = 0;
let truthyCount = 0;
for(let i = 0; i < array.length; i++){
let value = array[i];
sum += value;
if (value) {
truthyCount++;
}
}
// good
let array = [1, 2, 3];
let num = 1;
num += 1;
num -= 1;
const sum = array.reduce((a, b) => a + b, 0);
const truthyCount = array.filter(Boolean).length;
複製代碼
=
前/後換行。 若是你的語句超出 max-len
, 那就用()
把這個值包起來再換行。 eslint operator-linebreak
.// bad
const foo =
superLongLongLongLongLongLongLongLongFunctionName();
// bad
const foo
= 'superLongLongLongLongLongLongLongLongString';
// good
const foo = (
superLongLongLongLongLongLongLongLongFunctionName()
);
// good
const foo = 'superLongLongLongLongLongLongLongLongString';
複製代碼
no-unused-vars
// bad
var some_unused_var = 42;
// 定義了沒有使用
var y = 10;
y = 5;
// 不會將用於修改自身的讀取視爲已使用
var z = 0;
z = z + 1;
// 參數定義了但未使用
function getX(x, y) {
return x;
}
// good
function getXPlusY(x, y) {
return x + y;
}
var x = 1;
var y = a + 2;
alert(getXPlusY(x, y));
// 'type' 即便沒有使用也能夠被忽略, 由於這個有一個 rest 取值的屬性。
// 這是從對象中抽取一個忽略特殊字段的對象的一種形式
var { type, ...coords } = data;
// 'coords' 如今就是一個沒有 'type' 屬性的 'data' 對象
複製代碼
function example() {
console.log(notDefined); // => throws a ReferenceError
}
// 在變量聲明以前使用會正常輸出,是由於變量聲明提高,值沒有。
function example() {
console.log(declaredButNotAssigned); // => undefined
var declaredButNotAssigned = true;
}
// 表現同上
function example() {
let declaredButNotAssigned;
console.log(declaredButNotAssigned); // => undefined
declaredButNotAssigned = true;
}
// 用 const, let 不會發生提高
function example() {
console.log(declaredButNotAssigned); // => throws a ReferenceError
console.log(typeof declaredButNotAssigned); // => throws a ReferenceError
const declaredButNotAssigned = true;
}
複製代碼
var
狀況相同function example() {
console.log(anonymous); // => undefined
anonymous(); // => TypeError anonymous is not a function
var anonymous = function () {
console.log('anonymous function expression');
};
}
複製代碼
function example() {
console.log(named); // => undefined
named(); // => TypeError named is not a function
superPower(); // => ReferenceError superPower is not defined
var named = function superPower() {
console.log('Flying');
};
}
// 函數名和變量名相同也是同樣
function example() {
console.log(named); // => undefined
named(); // => TypeError named is not a function
var named = function named() {
console.log('named');
};
}
複製代碼
function example() {
superPower(); // => Flying
function superPower() {
console.log('Flying');
}
}
複製代碼
15.1 使用 ===
和 !==
而不是 ==
和 !=
. eslint: eqeqeq
15.2 if
等條件語句使用強制 ToBoolean
抽象方法來評估它們的表達式,而且始終遵循如下簡單規則:
Objects => true
Undefined => false
Null => false
Booleans => the value of the boolean
Numbers
Strings
''
=> falseif ([0] && []) {
// true
// 數組(即便是空數組)是對象,對象會計算成 true
}
複製代碼
// bad
if (isValid === true) {
// ...
}
// good
if (isValid) {
// ...
}
// bad
if (name) {
// ...
}
// good
if (name !== '') {
// ...
}
// bad
if (collection.length) {
// ...
}
// good
if (collection.length > 0) {
// ...
}
複製代碼
switch case
中,在 case
和 default
分句裏用大括號建立一個塊(如:let
, const
, function
, and class
). eslint rules: no-case-declarations
.詞彙聲明在整個 switch 塊中都是可見的,但只有在分配時纔會被初始化,這隻有在 case 達到時纔會發生。當多個 case 子句嘗試定義相同的事物時,會出現問題。
// bad
switch (foo) {
case 1:
let x = 1;
break;
case 2:
const y = 2;
break;
case 3:
function f() {
// ...
}
break;
default:
class C {}
}
// good
switch (foo) {
case 1: {
let x = 1;
break;
}
case 2: {
const y = 2;
break;
}
case 3: {
function f() {
// ...
}
break;
}
case 4:
bar();
break;
default: {
class C {}
}
}
複製代碼
eslint rules: no-nested-ternary
.
// bad
const foo = maybe1 > maybe2
? "bar"
: value1 > value2 ? "baz" : null;
// better
const maybeNull = value1 > value2 ? 'baz' : null;
const foo = maybe1 > maybe2
? 'bar'
: maybeNull;
// best
const maybeNull = value1 > value2 ? 'baz' : null;
const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
複製代碼
eslint rules: no-unneeded-ternary
.
// bad
const foo = a ? a : b;
const bar = c ? true : false;
const baz = c ? false : true;
// good
const foo = a || b;
const bar = !!c;
const baz = !c;
複製代碼
()
裏,只有當它們是標準的算術運算符(+
, -
, *
, & /
), 而且它們的優先級顯而易見時,能夠不用。 eslint: no-mixed-operators
// bad
const foo = a && b < 0 || c > 0 || d + 1 === 0;
// bad
const bar = a ** b - 5 % d;
// bad
if (a || b && c) {
return d;
}
// good
const foo = (a && b < 0) || c > 0 || (d + 1 === 0);
// good
const bar = (a ** b) - (5 % d);
// good
if (a || (b && c)) {
return d;
}
// good
const bar = a + b / c * d;
複製代碼
{}
包裹多行代碼塊。 eslint: nonblock-statement-body-position
// bad
if (test)
return false;
// good
if (test) return false;
// good
if (test) {
return false;
}
// bad
function foo() { return false; }
// good
function bar() {
return false;
}
複製代碼
else
和 if
的大括號保持在一行。 eslint: brace-style
// bad
if (test) {
thing1();
thing2();
}
else {
thing3();
}
// good
if (test) {
thing1();
thing2();
} else {
thing3();
}
複製代碼
if
語句都要用 return
返回, 那後面的 else
就不用寫了。 若是 if
塊中包含 return
, 它後面的 else if
塊中也包含了 return
, 這個時候就能夠把 else if
拆開。 eslint: no-else-return
// bad
function foo() {
if (x) {
return x;
} else {
return y;
}
}
// bad
function cats() {
if (x) {
return x;
} else if (y) {
return y;
}
}
// bad
function dogs() {
if (x) {
return x;
} else {
if (y) {
return y;
}
}
}
// good
function foo() {
if (x) {
return x;
}
return y;
}
// good
function cats() {
if (x) {
return x;
}
if (y) {
return y;
}
}
// good
function dogs(x) {
if (x) {
if (z) {
return y;
}
} else {
return z;
}
}
複製代碼
if
, while
等太長或者超過最大長度限制的時候,把每一個判斷條件放在單獨一行裏,邏輯運算符放在行首。// bad
if ((foo === 123 || bar === 'abc') && doesItLookGoodWhenItBecomesThatLong() && isThisReallyHappening()) {
thing1();
}
// bad
if (foo === 123 &&
bar === 'abc') {
thing1();
}
// bad
if (foo === 123
&& bar === 'abc') {
thing1();
}
// bad
if (
foo === 123 &&
bar === 'abc'
) {
thing1();
}
// good
if (
foo === 123
&& bar === 'abc'
) {
thing1();
}
// good
if (
(foo === 123 || bar === 'abc')
&& doesItLookGoodWhenItBecomesThatLong()
&& isThisReallyHappening()
) {
thing1();
}
// good
if (foo === 123 && bar === 'abc') {
thing1();
}
複製代碼
// bad
!isRunning && startRunning();
// good
if (!isRunning) {
startRunning();
}
複製代碼
/** ... */
// bad
// make() returns a new element
// based on the passed in tag name
//
// @param {String} tag
// @return {Element} element
function make(tag) {
// ...
return element;
}
// good
/** * make() returns a new element * based on the passed-in tag name */
function make(tag) {
// ...
return element;
}
複製代碼
//
,將單行註釋放在被註釋區域上方。若是註釋不是在第一行,就在註釋前面加一個空行// bad
const active = true; // is current tab
// good
// is current tab
const active = true;
// bad
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
const type = this._type || 'no type';
return type;
}
// good
function getType() {
console.log('fetching type...');
// set the default type to 'no type'
const type = this._type || 'no type';
return type;
}
// also good
function getType() {
// set the default type to 'no type'
const type = this._type || 'no type';
return type;
}
複製代碼
spaced-comment
// bad
//is current tab
const active = true;
// good
// is current tab
const active = true;
// bad
/** *make() returns a new element *based on the passed-in tag name */
function make(tag) {
// ...
return element;
}
// good
/** * make() returns a new element * based on the passed-in tag name */
function make(tag) {
// ...
return element;
}
複製代碼
FIXME' 或
TODO` 前綴, 這有助於其餘開發人員快速理解你指出的問題, 或者您建議的問題的解決方案。class Calculator extends Abacus {
constructor() {
super();
// FIXME: shouldn't use a global here
total = 0;
}
}
複製代碼
class Calculator extends Abacus {
constructor() {
super();
// TODO: total should be configurable by an options param
this.total = 0;
}
}
複製代碼
indent
// bad
function foo() {
∙∙∙∙const name;
}
// bad
function bar() {
∙const name;
}
// good
function baz() {
∙∙const name;
}
複製代碼
{}
前空一格。 eslint: space-before-blocks
// bad
function test(){
console.log('test');
}
// good
function test() {
console.log('test');
}
// bad
dog.set('attr',{
age: '1 year',
breed: 'Bernese Mountain Dog',
});
// good
dog.set('attr', {
age: '1 year',
breed: 'Bernese Mountain Dog',
});
複製代碼
if
, while
等的圓括號前空一格。在函數調用和定義時,函數名和圓括號之間不空格。 eslint: keyword-spacing
// bad
if(isJedi) {
fight ();
}
// good
if (isJedi) {
fight();
}
// bad
function fight () {
console.log ('Swooosh!');
}
// good
function fight() {
console.log('Swooosh!');
}
複製代碼
space-infix-ops
// bad
const x=y+5;
// good
const x = y + 5;
複製代碼
eol-last
// bad
import { es6 } from './AirbnbStyleGuide';
// ...
export default es6;
複製代碼
// bad
import { es6 } from './AirbnbStyleGuide';
// ...
export default es6;↵
↵
複製代碼
// good
import { es6 } from './AirbnbStyleGuide';
// ...
export default es6;↵
複製代碼
newline-per-chained-call
no-whitespace-before-property
// bad
$('#items').find('.selected').highlight().end().find('.open').updateCount();
// bad
$('#items').
find('.selected').
highlight().
end().
find('.open').
updateCount();
// good
$('#items')
.find('.selected')
.highlight()
.end()
.find('.open')
.updateCount();
// bad
const leds = stage.selectAll('.led').data(data).enter().append('svg:svg').classed('led', true)
.attr('width', (radius + margin) * 2).append('svg:g')
.attr('transform', `translate(${radius + margin},${radius + margin})`)
.call(tron.led);
// good
const leds = stage.selectAll('.led')
.data(data)
.enter().append('svg:svg')
.classed('led', true)
.attr('width', (radius + margin) * 2)
.append('svg:g')
.attr('transform', `translate(${radius + margin},${radius + margin})`)
.call(tron.led);
// good
const leds = stage.selectAll('.led').data(data);
複製代碼
// bad
if (foo) {
return bar;
}
return baz;
// good
if (foo) {
return bar;
}
return baz;
// bad
const obj = {
foo() {
},
bar() {
},
};
return obj;
// good
const obj = {
foo() {
},
bar() {
},
};
return obj;
// bad
const arr = [
function foo() {
},
function bar() {
},
];
return arr;
// good
const arr = [
function foo() {
},
function bar() {
},
];
return arr;
複製代碼
padded-blocks
// bad
function bar() {
console.log(foo);
}
// also bad
if (baz) {
console.log(qux);
} else {
console.log(foo);
}
// good
function bar() {
console.log(foo);
}
// good
if (baz) {
console.log(qux);
} else {
console.log(foo);
}
複製代碼
space-in-parens
// bad
function bar( foo ) {
return foo;
}
// good
function bar(foo) {
return foo;
}
// bad
if ( foo ) {
console.log(foo);
}
// good
if (foo) {
console.log(foo);
}
複製代碼
array-bracket-spacing
// bad
const foo = [ 1, 2, 3 ];
console.log(foo[ 0 ]);
// good, 逗號後面要加空格
const foo = [1, 2, 3];
console.log(foo[0]);
複製代碼
{}
里加空格。 eslint: object-curly-spacing
// bad
const foo = {clark: 'kent'};
// good
const foo = { clark: 'kent' };
// bad
function foo() {return true;}
if (foo) { bar = 0;}
// good
function foo() { return true; }
if (foo) { bar = 0; }
複製代碼
// bad
const foo = jsonData && jsonData.foo && jsonData.foo.bar && jsonData.foo.bar.baz && jsonData.foo.bar.baz.quux && jsonData.foo.bar.baz.quux.xyzzy;
// bad
$.ajax({ method: 'POST', url: 'https://airbnb.com/', data: { name: 'John' } }).done(() => console.log('Congratulations!')).fail(() => console.log('You have failed this city.'));
// good
const foo = jsonData
&& jsonData.foo
&& jsonData.foo.bar
&& jsonData.foo.bar.baz
&& jsonData.foo.bar.baz.quux
&& jsonData.foo.bar.baz.quux.xyzzy;
// good
$.ajax({
method: 'POST',
url: 'https://airbnb.com/',
data: { name: 'John' },
})
.done(() => console.log('Congratulations!'))
.fail(() => console.log('You have failed this city.'));
複製代碼
,
前避免空格, ,
後須要空格。 eslint: comma-spacing
// bad
var foo = 1,bar = 2;
var arr = [1 , 2];
// good
var foo = 1, bar = 2;
var arr = [1, 2];
複製代碼
key-spacing
// bad
var obj = { "foo" : 42 };
var obj2 = { "foo":42 };
// good
var obj = { "foo": 42 };
複製代碼
19.15 行末不要空格。 eslint: no-trailing-spaces
19.16 避免出現多個空行。 在文件末尾只容許空一行。 eslint: no-multiple-empty-lines
// bad
var x = 1;
var y = 2;
// good
var x = 1;
var y = 2;
複製代碼
comma-style
// bad
const story = [
once
, upon
, aTime
];
// good
const story = [
once,
upon,
aTime,
];
// bad
const hero = {
firstName: 'Ada'
, lastName: 'Lovelace'
, birthYear: 1815
, superPower: 'computers'
};
// good
const hero = {
firstName: 'Ada',
lastName: 'Lovelace',
birthYear: 1815,
superPower: 'computers',
};
複製代碼
comma-dangle
// bad - 沒有結尾逗號的 git diff
const hero = {
firstName: 'Florence',
- lastName: 'Nightingale'
+ lastName: 'Nightingale',
+ inventorOf: ['coxcomb chart', 'modern nursing']
};
// good - 有結尾逗號的 git diff
const hero = {
firstName: 'Florence',
lastName: 'Nightingale',
+ inventorOf: ['coxcomb chart', 'modern nursing'],
};
複製代碼
// bad
const hero = {
firstName: 'Dana',
lastName: 'Scully'
};
const heroes = [
'Batman',
'Superman'
];
// good
const hero = {
firstName: 'Dana',
lastName: 'Scully',
};
const heroes = [
'Batman',
'Superman',
];
// bad
function createHero( firstName, lastName, inventorOf ) {
// does nothing
}
// good
function createHero( firstName, lastName, inventorOf, ) {
// does nothing
}
// good (note that a comma must not appear after a "rest" element)
function createHero( firstName, lastName, inventorOf, ...heroArgs ) {
// does nothing
}
// bad
createHero(
firstName,
lastName,
inventorOf
);
// good
createHero(
firstName,
lastName,
inventorOf,
);
// good (note that a comma must not appear after a "rest" element)
createHero(
firstName,
lastName,
inventorOf,
...heroArgs
)
複製代碼
Automatic Semicolon Insertion
這一規則來決定行末是否加分號。可是,ASI 包含一些古怪的行爲,若是 JavaScript 弄錯了你的換行符,你的代碼就會破壞。因此明確地使用分號,會減小這種不肯定性。// bad
(function () {
const name = 'Skywalker'
return name
})()
// good
(function () {
const name = 'Skywalker';
return name;
}());
// good
;(() => {
const name = 'Skywalker';
return name;
}());
複製代碼
更多.
22.1 在聲明開頭執行強制類型轉換。
22.2 String eslint: no-new-wrappers
// => this.reviewScore = 9;
// bad
const totalScore = new String(this.reviewScore); // typeof totalScore is "object" not "string"
// bad
const totalScore = this.reviewScore + ''; // invokes this.reviewScore.valueOf()
// bad
const totalScore = this.reviewScore.toString(); // 不保證返回string
// good
const totalScore = String(this.reviewScore);
複製代碼
radix
const inputValue = '4';
// bad
const val = new Number(inputValue);
// bad
const val = +inputValue;
// bad
const val = inputValue >> 0;
// bad
const val = parseInt(inputValue);
// good
const val = Number(inputValue);
// good
const val = parseInt(inputValue, 10);
複製代碼
parseInt
是你的性能瓶頸致使你必定要用移位運算。 請說明這個是由於性能緣由,// good
/** * parseInt 致使代碼運行慢 * Bitshifting the String 將其強制轉換爲數字使其快得多。 */
const val = inputValue >> 0;
複製代碼
2147483647 >> 0 //=> 2147483647
2147483648 >> 0 //=> -2147483648
2147483649 >> 0 //=> -2147483647
複製代碼
const age = 0;
// bad
const hasAge = new Boolean(age);
// good
const hasAge = Boolean(age);
// best
const hasAge = !!age;
複製代碼
id-length
// bad
function q() {
// ...
}
// good
function query() {
// ...
}
複製代碼
camelcase
// bad
const OBJEcttsssss = {};
const this_is_my_object = {};
function c() {}
// good
const thisIsMyObject = {};
function thisIsMyFunction() {}
複製代碼
new-cap
// bad
function user(options) {
this.name = options.name;
}
const bad = new user({
name: 'nope',
});
// good
class User {
constructor(options) {
this.name = options.name;
}
}
const good = new User({
name: 'yup',
});
複製代碼
no-underscore-dangle
JavaScript 沒有私有屬性或方法的概念。儘管前置下劃線一般的概念上意味着 「private」,但其實,這些屬性是徹底公開的,所以這部分也是你的 API 的內容。這一律念可能會致使開發者誤覺得更改這個不會致使崩潰或者不須要測試。
// bad
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';
this._firstName = 'Panda';
// good
this.firstName = 'Panda';
複製代碼
this
的引用,使用箭頭函數或硬綁定。// bad
function foo() {
const self = this;
return function () {
console.log(self);
};
}
// bad
function foo() {
const that = this;
return function () {
console.log(that);
};
}
// good
function foo() {
return () => {
console.log(this);
};
}
複製代碼
export default
)的名稱徹底匹配// file 1 contents
class CheckBox {
// ...
}
export default CheckBox;
// file 2 contents
export default function fortyTwo() { return 42; }
// file 3 contents
export default function insideDirectory() {}
// in some other file
// bad
import CheckBox from './checkBox'; // PascalCase import/export, camelCase filename
import FortyTwo from './FortyTwo'; // PascalCase import/filename, camelCase export
import InsideDirectory from './InsideDirectory'; // PascalCase import/filename, camelCase export
// bad
import CheckBox from './check_box'; // PascalCase import/export, snake_case filename
import forty_two from './forty_two'; // snake_case import/filename, camelCase export
import inside_directory from './inside_directory'; // snake_case import, camelCase export
import index from './inside_directory/index'; // requiring the index file explicitly
import insideDirectory from './insideDirectory/index'; // requiring the index file explicitly
// good
import CheckBox from './CheckBox'; // PascalCase export/import/filename
import fortyTwo from './fortyTwo'; // camelCase export/import/filename
import insideDirectory from './insideDirectory'; // camelCase export/import/directory name/implicit "index"
// ^ supports both insideDirectory.js and insideDirectory/index.js
複製代碼
export default
)一個函數時,函數名、文件名統一。function makeStyleGuide() {
// ...
}
export default makeStyleGuide;
複製代碼
const AirbnbStyleGuide = {
es6: {
}
};
export default AirbnbStyleGuide;
複製代碼
名字是給人看的,不是給電腦看的。
// bad
import SmsContainer from './containers/SmsContainer';
// bad
const HttpRequests = [
// ...
];
// good
import SMSContainer from './containers/SMSContainer';
// good
const HTTPRequests = [
// ...
];
// best
import TextMessageContainer from './containers/TextMessageContainer';
// best
const Requests = [
// ...
];
複製代碼
// bad
const PRIVATE_VARIABLE = 'should not be unnecessarily uppercased within a file';
// bad
export const THING_TO_BE_CHANGED = 'should obviously not be uppercased';
// bad
export let REASSIGNABLE_VARIABLE = 'do not use let with uppercase variables';
// ---
// allowed but does not supply semantic value
export const apiKey = 'SOMEKEY';
// better in most cases
export const API_KEY = 'SOMEKEY';
// ---
// bad - unnecessarily uppercases key while adding no semantic value
export const MAPPING = {
KEY: 'value'
};
// good
export const MAPPING = {
key: 'value'
};
複製代碼
24.1 不須要使用屬性的訪問器函數。
24.2 不要使用 JavaScript 的 getters/setters,由於他們會產生反作用,而且難以測試、維護和理解。若是必要,你能夠用 getVal()和 setVal() 去構建。
// bad
class Dragon {
get age() {
// ...
}
set age(value) {
// ...
}
}
// good
class Dragon {
getAge() {
// ...
}
setAge(value) {
// ...
}
}
複製代碼
boolean
, 請用 isVal()
或 hasVal()
。// bad
if (!dragon.age()) {
return false;
}
// good
if (!dragon.hasAge()) {
return false;
}
複製代碼
class Jedi {
constructor(options = {}) {
const lightsaber = options.lightsaber || 'blue';
this.set('lightsaber', lightsaber);
}
set(key, val) {
this[key] = val;
}
get(key) {
return this[key];
}
}
複製代碼
// bad
$(this).trigger('listingUpdated', listing.id);
...
$(this).on('listingUpdated', (e, listingId) => {
// do something with listingId
});
複製代碼
prefer:
// good
$(this).trigger('listingUpdated', { listingId: listing.id });
...
$(this).on('listingUpdated', (e, data) => {
// do something with data.listingId
});
複製代碼
所謂規範,更多的仍是爲了代碼的可讀性,畢竟咱們的代碼更重要的是給人看。同時,合理的規範,也會幫助咱們規避不少沒必要要的 bug。
關注微信公衆號:前端發動機,回覆:加羣。
若是你看到了這裏,且本文對你有一點幫助的話,但願你能夠動動小手支持一下做者,感謝🍻。文中若有不對之處,也歡迎你們指出,共勉。好了,又耽誤你們的時間了,感謝閱讀,下次再見!
感興趣的同窗能夠關注下個人公衆號 前端發動機,好玩又有料。