前端代碼規範 — JavaScript 風格指南

前言

本文基於 github 項目 airbnb/javascript 翻譯,也加入了一些我的理解。規範有利於咱們更好的提升代碼可讀性,避免一些沒必要要的 bug。可是,並無統一的標準和硬性要求,這裏只是給你們提供一些參考,適合團隊和本身的纔是最好的。javascript

我的博客地址 🍹🍰 fe-codecss

類型

  • 1.1 基本類型
基本類型賦值時,應該直接使用類型的值
  • 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 只能阻止引用類型地址的從新賦值
// 並不能保證引用類型的屬性等不變

狀態的使用(原文爲 Reference)

儘可能確保你的代碼中的狀態是可控範圍內的,重複引用會出現難以理解的 bug 和代碼。
// bad
var a = 1;
var b = 2;

// good
const a = 1;
const b = 2;
  • 2.2 若是你必定要對參數從新賦值,那就用let,而不是var. eslint: no-var
let是塊級做用域, var是函數級做用域,一樣是爲了減小代碼的不可控,減小 「意外」
// bad
var count = 1;
if (true) {
  count += 1;
}

// good, use the let.
let count = 1;
if (true) {
  count += 1;
}
  • 2.3 letconst都是塊級做用域
// const 和 let 都只存在於它定義的那個塊級做用域
{
  let a = 1;
  const b = 1;
}
console.log(a); // ReferenceError
console.log(b); // ReferenceError

對象

// bad
const item = new Object();

// good
const item = {};
  • 3.2 當建立一個帶有動態屬性名的對象時,將定義的全部屬性放在對象的一個地方。
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,
};
// bad
const atom = {
  value: 1,

  addValue: function (value) {
    return atom.value + value;
  },
};

// good
const atom = {
  value: 1,

  // 對象的方法
  addValue(value) {
    return atom.value + value;
  },
};
const lukeSkywalker = 'Luke Skywalker';

// bad
const obj = {
  lukeSkywalker: lukeSkywalker,
};

// good
const obj = {
  lukeSkywalker
};
  • 3.5 將屬性的縮寫放在對象聲明的開頭。
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,
};
  • 3.6 只對那些無效的標示使用引號 ''. eslint: quote-props
通常來講,咱們認爲它在主觀上更容易閱讀。它改進了語法突出顯示,而且更容易被JS引擎優化。
// bad
const bad = {
  'foo': 3,
  'bar': 4,
  'data-blah': 5,
};

// good
const good = {
  foo: 3,
  bar: 4,
  'data-blah': 5,
};
  • 3.7 不要直接調用Object.prototype上的方法,如hasOwnProperty, propertyIsEnumerable, isPrototypeOf
在一些有問題的對象上, 這些方法可能會被屏蔽掉 - 如: { hasOwnProperty: false } - 或這是一個空對象 Object.create(null)
// 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));
  • 3.8 對象淺拷貝時,更推薦使用擴展運算符 ...,而不是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 }

數組

// bad
const items = new Array();

// good
const items = [];
  • 4.2 用Array#push 向數組中添加一個值而不是直接用下標。
const someStack = [];

// bad
someStack[someStack.length] = 'abracadabra';

// good
someStack.push('abracadabra');
  • 4.3 用擴展運算符作數組淺拷貝,相似上面的對象淺拷貝
// bad
const len = items.length;
const itemsCopy = [];
let i;

for (i = 0; i < len; i += 1) {
  itemsCopy[i] = items[i];
}

// good
const itemsCopy = [...items];
  • 4.4 推薦用 ... 運算符而不是Array.from來將一個類數組轉換成數組。
const foo = document.querySelectorAll('.foo');

// good
const nodes = Array.from(foo);

// best
const nodes = [...foo];
  • 4.5 用 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);
  • 4.6 用 Array.from 而不是 ... 運算符去迭代。 這樣能夠避免建立一箇中間數組。
// bad
const baz = [...foo].map(bar);

// good
const baz = Array.from(foo, bar);
  • 4.7 在數組方法的回調函數中使用 return 語句。 若是函數體由一條返回一個表達式的語句組成, 而且這個表達式沒有反作用, 這個時候能夠忽略return,詳見 8.2. eslint: 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;
});
  • 4.8 若是一個數組有不少行,在數組的 [ 後和 ] 前換行。
// 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,
];

解構

  • 5.1 用對象的解構賦值來獲取和使用對象某個或多個屬性值。 eslint: prefer-destructuring
這樣就不須要給這些屬性建立臨時/引用
// 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}`;
}
  • 5.2 數組解構.
const arr = [1, 2, 3, 4];

// bad
const first = arr[0];
const second = arr[1];

// good
const [first, second] = arr;
  • 5.3 多個返回值用對象的解構,而不是數組解構。
不依賴於返回值的順序,更可讀
// 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);

字符串

  • 6.1 string 統一用單引號 '' 。 eslint: quotes
// bad
const name = "Capt. Janeway";

// bad - 模板應該包含插入文字或換行
const name = `Capt. Janeway`;

// good
const name = 'Capt. Janeway';
  • 6.2 不該該用 + 鏈接換行字符串。
很差用,且可讀性差
// 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.';
模板字符串更具可讀性、語法簡潔、字符串插入參數。
// 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}?`;
}
  • 6.4 永遠不要在字符串中用eval(),漏洞太多。 eslint: no-eval
反斜線可讀性差,只在必要時使用
// bad
const foo = '\'this\' \i\s \"quoted\"';

// good
const foo = '\'this\' is "quoted"';

//best
const foo = `my name is '${name}'`;

函數

  • 7.1 用命名函數表達式而不是函數聲明。eslint: func-style
函數聲明做用域會提高,下降了代碼可讀性和可維護性。若是你發現一個函數又大又複雜,這個函數妨礙這個文件其餘部分的理解性,這可能就是時候把這個函數單獨抽成一個模塊了。( Discussion)
// bad
function foo() {
  // ...
}

// bad
const foo = function () {
  // ...
};

// good

const short = function longUniqueMoreDescriptiveLexicalFoo() {
  // ...
};
  • 7.2 把當即執行函數包裹在圓括號裏。 eslint: 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.');
  };
}
  • 7.5 永遠不要用arguments命名參數。它的優先級高於每一個函數做用域自帶的 arguments 對象, 因此會致使函數自帶的 arguments 值被覆蓋。
// bad
function foo(name, options, arguments) {
  // ...
}

// good
function foo(name, options, args) {
  // ...
}
... 更明確你想用哪些參數。
// bad
function concatenateAll() {
  const args = Array.prototype.slice.call(arguments);
  return args.join('');
}

// good
function concatenateAll(...args) {
  return args.join('');
}
  • 7.8 使用默認參數語法,而不是在函數裏對參數從新賦值。
// really bad
function handleThings(opts) {
  // 雖然你想這麼寫, 可是這個會帶來一些細微的bug
  // 若是 opts 的值爲 false, 它會被賦值爲 {}
  opts = opts || {};
  // ...
}

// still bad
function handleThings(opts) {
  if (opts === void 0) {
    opts = {};
  }
  // ...
}

// good
function handleThings(opts = {}) {
  // ...
}
  • 7.8 使用默認參數時,須要避免反作用
var b = 1;
// bad
function count(a = b++) {
  console.log(a);
}
count();  // 1
count();  // 2
count(3); // 3
count();  // 3
// 很容易讓人懵逼
  • 7.9 把默認參數賦值放在最後
// bad
function handleThings(opts = {}, name) {
  // ...
}

// good
function handleThings(name, opts = {}) {
  // ...
}
  • 7.10 不要用 Function 建立函數。 eslint: no-new-func
// bad
var add = new Function('a', 'b', 'return a + b');

// still bad
var subtract = Function('a', 'b', 'return a - b');
// bad
const f = function(){};
const g = function (){};
const h = function() {};

// good
const x = function () {};
const y = function a() {};
特別注意引用類型的操做,保證數據的不可變性
// bad
function f1(obj) {
  obj.key = 1;
};

// good
function f2(obj) {
  const key = Object.prototype.hasOwnProperty.call(obj, 'key') ? obj.key : 1;
};
// 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) {
  // ...
}
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]);
  • 7.15 多個參數的函數應該像這個指南里的其餘多行代碼寫法同樣: 每行只有一個參數,每行逗號結尾。
// bad
function foo(bar,
             baz,
             quux) {
  // ...
}

// good
function foo(
  bar,
  baz,
  quux,
) {
  // ...
}

// bad
console.log(foo,
  bar,
  baz);

// good
console.log(
  foo,
  bar,
  baz,
);

箭頭函數

它建立了一個在上下文中執行的函數,這一般是您想要的,而且是一種更簡潔的語法。
// 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;
});
  • 8.2 若是函數體由一個沒有反作用的表達式的單個語句組成,去掉大括號和 return。不然,保留大括號且使用 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;
});
  • 8.3 若是表達式有多行,首尾放在圓括號裏更可讀。
// bad
['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
    httpMagicObjectWithAVeryLongName,
    httpMethod
  )
);

// good
['get', 'post', 'put'].map(httpMethod => (
  Object.prototype.hasOwnProperty.call(
    httpMagicObjectWithAVeryLongName,
    httpMethod
  )
));
  • 8.4 爲了清晰和一致,始終在參數周圍加上括號 eslint: 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;
});
// 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;
};
// bad
(foo) =>
  bar;

(foo) =>
  (bar);

// good
(foo) => bar;
(foo) => (bar);
(foo) => (
   bar
)

類和構造函數

  • 9.1 始終用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;
  }
}
  • 9.2 使用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];
  }
}
  • 9.3 方法能夠返回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);
  • 9.4 容許寫一個自定義的 toString() 方法,可是要保證它是能夠正常工做且沒有反作用
class Jedi {
  constructor(options = {}) {
    this.name = options.name || 'no name';
  }

  getName() {
    return this.name;
  }

  toString() {
    return `Jedi - ${this.getName()}`;
  }
}
  • 9.5 若是沒有特殊說明,類有默認的構造方法。不用特地寫一個空的構造函數或只是表明父類的構造函數。 eslint: 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';
  }
}
重複類成員會默默的執行最後一個,有重複確定就是一個錯誤
// bad
class Foo {
  bar() { return 1; }
  bar() { return 2; }
}

// good
class Foo {
  bar() { return 1; }
}

// good
class Foo {
  bar() { return 2; }
}

模塊

  • 10.1 在非標準模塊系統上使用(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;
  • 10.2 不要用 import * 這種通配符
// bad
import * as AirbnbStyleGuide from './AirbnbStyleGuide';

// good
import AirbnbStyleGuide from './AirbnbStyleGuide';
  • 10.3 不要直接從 import 中直接 export
看起來簡潔,可是影響可讀性
// bad
// filename es6.js
export { es6 as default } from './AirbnbStyleGuide';

// good
// filename es6.js
import { es6 } from './AirbnbStyleGuide';
export default es6;
  • 10.4 一個入口只 import 一次。

eslint: no-duplicate-importshtml

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';
  • 10.5 不要導出可變的綁定

eslint: import/no-mutable-exports前端

儘可能減小狀態,保證數據的不可變性。雖然在某些場景下可能須要這種技術,但總的來講應該導出常量。
// bad
let foo = 3;
export { foo }

// good
const foo = 3;
export { foo }
  • 10.6 在只有一個導出的模塊裏,用 export default 更好。

eslint: import/prefer-default-exportvue

鼓勵使用更多文件,每一個文件只作一件事情並導出,這樣可讀性和可維護性更好。
// bad
export function foo() {}

// good
export default function foo() {}
  • 10.7 import 放在其餘全部語句以前。

eslint: import/firstjava

防止意外行爲。
// bad
import foo from 'foo';
foo.init();

import bar from 'bar';

// good
import foo from 'foo';
import bar from 'bar';

foo.init();
  • 10.8 多行 import 應該縮進,就像多行數組和對象字面量
// bad
import {longNameA, longNameB, longNameC, longNameD, longNameE} from 'path';

// good
import {
  longNameA,
  longNameB,
  longNameC,
  longNameD,
  longNameE,
} from 'path';
  • 10.9 在 import 語句裏不容許 Webpack loader 語法

eslint: import/no-webpack-loader-syntaxnode

最好是在 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';

迭代器和生成器

不可變原則,處理純函數的返回值比處理反作用更容易。

數組的迭代方法: map() / every() / filter() / find() / findIndex() / reduce() / some() / ... , 對象的處理方法 :Object.keys() / Object.values() / Object.entries() 去產生一個數組, 這樣你就能去遍歷對象了。webpack

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);
  • 11.2 如今不要用 generator
兼容性很差
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* () {
  // ...
}

屬性

const luke = {
  jedi: true,
  age: 28,
};

// bad
const isJedi = luke['jedi'];

// good
const isJedi = luke.jedi;
  • 12.2 獲取的屬性是變量時用方括號[]
const luke = {
  jedi: true,
  age: 28,
};

function getProp(prop) {
  return luke[prop];
}

const isJedi = getProp('jedi');
// bad
const binary = Math.pow(2, 10);

// good
const binary = 2 ** 10;

變量

  • 13.1 始終用 constlet 聲明變量。若是你不想遇到一對變量提高、全局變量的 bug 的話。 eslint: no-undef prefer-const
// bad
superPower = new SuperPower();

// good
const superPower = new SuperPower();
  • 13.2 每一個變量單獨用一個 constlet 。 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';
  • 13.3 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;
  • 13.4 變量聲明放在合理的位置
// 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;
}
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` 也同樣
  • 13.6 不要使用一元遞增遞減運算符(++--). 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;
  • 13.7 避免在 = 前/後換行。 若是你的語句超出 max-len, 那就用()把這個值包起來再換行。 eslint operator-linebreak.
// bad
const foo =
  superLongLongLongLongLongLongLongLongFunctionName();

// bad
const foo
  = 'superLongLongLongLongLongLongLongLongString';

// good
const foo = (
  superLongLongLongLongLongLongLongLongFunctionName()
);

// good
const foo = 'superLongLongLongLongLongLongLongLongString';
// 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;
}
  • 14.2 匿名函數表達式和 var 狀況相同
function example() {
  console.log(anonymous); // => undefined

  anonymous(); // => TypeError anonymous is not a function

  var anonymous = function () {
    console.log('anonymous function expression');
  };
}
  • 14.3 已命名的函數表達式提高他的變量名,而不是函數名或函數體
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');
  };
}
  • 14.4 函數聲明則提高了函數名和函數體
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
  • Numbersgit

    • +0, -0, or NaN => false
    • 其餘 => true
  • Stringses6

    • '' => false
    • 其餘 => true
if ([0] && []) {
  // true
  // 數組(即便是空數組)是對象,對象會計算成 true
}
  • 15.3 布爾值比較能夠省略,可是字符串和數字要顯示比較
// bad
if (isValid === true) {
  // ...
}

// good
if (isValid) {
  // ...
}

// bad
if (name) {
  // ...
}

// good
if (name !== '') {
  // ...
}

// bad
if (collection.length) {
  // ...
}

// good
if (collection.length > 0) {
  // ...
}
  • 15.4 switch case 中,在 casedefault 分句裏用大括號建立一個塊(如: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 {}
  }
}
  • 15.5 三元表達式不該該嵌套,一般是單行表達式。

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;
  • 15.7 避免不須要的三元表達式

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;
  • 15.8 混合操做符時,要放在 () 裏,只有當它們是標準的算術運算符(+, -, *, & /), 而且它們的優先級顯而易見時,能夠不用。 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;

// 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;
}
  • 16.2 elseif 的大括號保持在一行。 eslint: brace-style
// bad
if (test) {
  thing1();
  thing2();
}
else {
  thing3();
}

// good
if (test) {
  thing1();
  thing2();
} else {
  thing3();
}
  • 16.3 若是 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;
  }
}

控制

  • 17.1 當你的控制語句 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();
}
  • 17.2 不要用選擇操做符代替控制語句。
// bad
!isRunning && startRunning();

// good
if (!isRunning) {
  startRunning();
}

註釋

  • 18.1 多行註釋用 /** ... */
// 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;
}
  • 18.2 單行註釋用//,將單行註釋放在被註釋區域上方。若是註釋不是在第一行,就在註釋前面加一個空行
// 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;
}
  • 18.3 全部註釋開頭加一個空格,方便閱讀。 eslint: 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;
}
  • 18.4 在註釋前加上 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;
  }
}

空格

  • 19.1 Tab 使用兩個空格(或者 4 個,你開心就好,可是團隊統一是必須的)。 eslint: indent
// bad
function foo() {
∙∙∙∙const name;
}

// bad
function bar() {
∙const name;
}

// good
function baz() {
∙∙const name;
}
// 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',
});
  • 19.3 在控制語句 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!');
}
// bad
const x=y+5;

// good
const x = y + 5;
  • 19.5 文件結尾空一行. eslint: 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;↵
// 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);
  • 19.7 在一個代碼塊以後,下一條語句以前空一行。
// 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;
  • 19.8 不要故意留一些不必的空白行。 eslint: 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);
}
// bad
function bar( foo ) {
  return foo;
}

// good
function bar(foo) {
  return foo;
}

// bad
if ( foo ) {
  console.log(foo);
}

// good
if (foo) {
  console.log(foo);
}
// bad
const foo = [ 1, 2, 3 ];
console.log(foo[ 0 ]);

// good, 逗號後面要加空格
const foo = [1, 2, 3];
console.log(foo[0]);
// 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; }
  • 19.12 避免一行代碼超過 100 個字符(包含空格、純字符串就不要換行了)。
// 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.'));
  • 19.13 , 前避免空格, , 後須要空格。 eslint: comma-spacing
// bad
var foo = 1,bar = 2;
var arr = [1 , 2];

// good
var foo = 1, bar = 2;
var arr = [1, 2];
  • 19.14 在對象的屬性中, 鍵值之間要有空格。 eslint: key-spacing
// bad
var obj = { "foo" : 42 };
var obj2 = { "foo":42 };

// good
var obj = { "foo": 42 };

<!-- markdownlint-disable MD012 -->

// bad
var x = 1;

var y = 2;

// good
var x = 1;

var y = 2;

逗號

// 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',
};
  • 20.2 結尾額外加逗號,看團隊習慣吧 eslint: 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
)

分號

  • 21.1 當 JavaScript 遇到沒有分號的換行符時,它會使用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);
  • 22.3 Number eslint: 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);
  • 22.4 請在註釋中解釋爲何要用移位運算,不管你在作什麼,好比因爲 parseInt 是你的性能瓶頸致使你必定要用移位運算。 請說明這個是由於性能緣由,
// good
/**
 * parseInt 致使代碼運行慢
 * Bitshifting the String 將其強制轉換爲數字使其快得多。
 */
const val = inputValue >> 0;
  • 22.5 注意: 使用 bitshift 操做時要當心。數字表示爲 64 位值,但 bitshift 操做始終返回 32 位整數。對於大於32位的整數值,Bitshift可能會致使意外行爲。
2147483647 >> 0 //=> 2147483647
2147483648 >> 0 //=> -2147483648
2147483649 >> 0 //=> -2147483647
  • 22.6 Booleans
const age = 0;

// bad
const hasAge = new Boolean(age);

// good
const hasAge = Boolean(age);

// best
const hasAge = !!age;

命名約定

  • 23.1 避免用一個字母命名,讓你的命名更加語義化。 eslint: id-length
// bad
function q() {
  // ...
}

// good
function query() {
  // ...
}
  • 23.2 用 camelCase 命名你的對象、函數、實例。 eslint: camelcase
// bad
const OBJEcttsssss = {};
const this_is_my_object = {};
function c() {}

// good
const thisIsMyObject = {};
function thisIsMyFunction() {}
  • 23.3 用 PascalCase 命名類。 eslint: 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',
});
JavaScript 沒有私有屬性或方法的概念。儘管前置下劃線一般的概念上意味着 「private」,但其實,這些屬性是徹底公開的,所以這部分也是你的 API 的內容。這一律念可能會致使開發者誤覺得更改這個不會致使崩潰或者不須要測試。
// bad
this.__firstName__ = 'Panda';
this.firstName_ = 'Panda';
this._firstName = 'Panda';

// good
this.firstName = 'Panda';
  • 23.5 不要保存 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);
  };
}
  • 23.6 文件名應與默認導出(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
  • 23.7 默認導出(export default)一個函數時,函數名、文件名統一。
function makeStyleGuide() {
  // ...
}

export default makeStyleGuide;
  • 23.8 當你 export 一個構造函數/類/單例/函數庫對象時用 PascalCase。
const AirbnbStyleGuide = {
  es6: {
  }
};

export default AirbnbStyleGuide;
  • 23.9 簡稱和首字母縮寫應該所有大寫或所有小寫。
名字是給人看的,不是給電腦看的。
// 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 = [
  // ...
];
  • 23.10 全大寫字母定義用來導出的常量
// 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) {
    // ...
  }
}
  • 24.3 若是屬性/方法是一個 boolean, 請用 isVal()hasVal()
// bad
if (!dragon.age()) {
  return false;
}

// good
if (!dragon.hasAge()) {
  return false;
}
  • 24.4 能夠用 get() 和 set() 函數,可是要保持一致。
class Jedi {
  constructor(options = {}) {
    const lightsaber = options.lightsaber || 'blue';
    this.set('lightsaber', lightsaber);
  }

  set(key, val) {
    this[key] = val;
  }

  get(key) {
    return this[key];
  }
}

Events

  • 25.1 給事件或其餘傳遞數據時,不直接使用原始值,而是經過對象包裝。這樣在將來須要增長或減小參數,沒必要找到每一個使用中的處理器。
// 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。

交流羣

關注微信公衆號:前端發動機,回覆:加羣。

後記

若是你看到了這裏,且本文對你有一點幫助的話,但願你能夠動動小手支持一下做者,感謝🍻。文中若有不對之處,也歡迎你們指出,共勉。好了,又耽誤你們的時間了,感謝閱讀,下次再見!

感興趣的同窗能夠關注下個人公衆號 前端發動機,好玩又有料。

相關文章
相關標籤/搜索