提供一種合理的javascript的規範,對原文主要內容進行翻譯,同時對部份內容進行註釋。推薦你們先收藏,在寫代碼的時候能夠方便參考。javascript
注意:本文假定你正在使用 Babel,而且要求你使用 babel-preset-airbnb或者其替代品。同時,假定你已經經過 airbnb-browser-shims或者其替代品安裝 shims/polyfills 在你的app內。
若是您想閱讀原文?css
若是您想在github上查看?java
若是您想了解並使用 babel with airbnb?node
簡單的基本數據類型,直接使用其值react
- `string` - `number` - `boolean` - `null` - `undefined` - `symbol`
const foo = 1; let bar = foo; bar = 9; console.log(foo, bar); // => 1, 9
複雜的基本數據類型,直接使用其值的引用jquery
- `object` - `array` - `function`
const foo = [1, 2]; const bar = foo; bar[0] = 9; console.log(foo[0], bar[0]); // => 9, 9
symbol
自ES6引入,目的是提供一種機制,保證每一個屬性名都是惟一的,從根本上防止屬性名的衝突。在這以前,對象屬性名都是字符串。其實看到這裏,string
和symbol
類型有點class
和id
的意思webpack
Symbol()
的聲明,由於 Symbol()
返回值是一個相似於字符串的基本類型,不是一個對象,因此不能使用 new
命令git
let ylone = Symbol(); typeof(ylone); ? "symbol" //爲聲明加上描述 let ylone1 = Symbol('hello'); ylone1; ? Symbol(hello);
不管是不加描述,仍是所加的描述相同, Symbol()
函數的返回值都不相同es6
Symbol.for('key')
也會返回一個Symbol,可是Symbol.for()
採用登記機制(會被登記在全局環境中供搜索),若是以前key
已經存在,則直接返回該值,不然新建一個值。好比,若是你調用 Symbol.for("cat")
30 次,每次都會返回同一個Symbol值,可是調用Symbol("cat")
30 次,會返回 30 個不一樣的Symbol值。github
Symbol
自己不能與其餘值進行運算,可是能夠轉換成字符串和布爾類型
對象中使用Symbol()
。經過對比以前經過 a['string']
的方式,至關於多了一步轉換,來保證屬性命名的安全。
let mySymbol = Symbol(); // 第一種寫法 let a = {}; a[mySymbol] = 'Hello!'; // 第二種寫法 let a = { [mySymbol]: 'Hello!' }; // 第三種寫法 let a = {}; Object.defineProperty(a, mySymbol, { value: 'Hello!' }); a[mySymbol] ? 'hello!'
注意,因爲 .
運算符後面老是字符串,因此Symbol()
不支持點式聲明對象屬性。在對象內部使用 [symbol]
這樣的寫法也是這個道理
const
而不用 var
,這樣能夠保證你聲明的值不會被重定義// bad var a = 1; var b = 2; // good const a = 1; const b = 2;
let
而不是var
,由於 let
是塊級做用域元素, var
是函數做用域元素// bad var count = 1; if (true) { count += 1; } // good, use the let. let count = 1; if (true) { count += 1; }
let
和const
都是塊級做用域函數,他們都只存在於他們被定義的塊中// const and let only exist in the blocks they are defined in. { let a = 1; const b = 1; } console.log(a); // ReferenceError console.log(b); // ReferenceError
塊級做用域的常量,此聲明建立一個常量,其做用域能夠是全局或本地聲明的塊。聲明時須要指定其值做爲一個常數的初始化器。通常狀況下, const
聲明的值不能改變,可是對象元素能夠改變其屬性,數組元素能夠向其中添加值,可是不能從新賦值
const a = 100; a = 10; ? Uncaught TypeError: Assignment to constant variable const a = []; a.push('a'); ✔ a = ['a']; ? Uncaught TypeError: Assignment to constant variable const obj = {'name':'ylone'}; obj['name'] = 'yh'; ✔ obj = {'name':'yh'}; ? Uncaught TypeError: Assignment to constant variable
注意,chrome30嚴格模式下不能使用,const(Uncaught SyntaxError: Use of const in strict mode. )
let容許你聲明一個做用域被限制在塊級中的變量、語句或者表達式。let聲明的變量只在其聲明的塊或子塊中可用,這一點,與var類似。兩者之間最主要的區別在於var聲明的變量的做用域是整個封閉函數。
var q = 1; var w = 2; if(true){ var q = 11; let w = 22; console.log(q,w); ?(11,22) } console.log(q,w); ?(11,2)
在其餘類C語言中,由 {}
封閉的代碼塊即爲 block-scoped
,{..block-scoped..}
if(true){ var a = 100; } a; ? 100 if(true){ let b = 100; } b; ? Uncaught ReferenceError: b is not defined
若是是類C語言中,a
會在if語句執行完畢後銷燬,可是在javascript中,if中的變量聲明會將變臉那個添加到當前的執行環境中,這裏能夠看出 var與let的區別
,var
聲明的變量會自動被添加到最接近的執行環境中,let
聲明的變量則只會存在與塊級做用域中
函數做用域,每一個函數被聲明時的上下文執行環境,fucnction(){..function-scoped..}
{}
來建立對象,由於這樣更加簡潔,性能上和 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 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, };
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, };
[]
標記爲符號類型。這樣作的好處在於,加強代碼高亮,方便閱讀,而且對js引擎更加友好// bad const bad = { 'foo': 3, 'bar': 4, 'data-blah': 5, }; // good const good = { foo: 3, bar: 4, 'data-blah': 5, };
Object.prototype
下的方法,好比 hasOwnProperty
,isPrototypeOf
,propertyIsEnumerable
等,由於這些方法可能被覆蓋{ hasOwnProperty: false }
,或者對象爲空報錯// bad console.log(object.hasOwnProperty(key)); // good console.log(Object.prototype.hasOwnProperty.call(object, key)); // best const has = Object.prototype.hasOwnProperty; // cache the lookup once, in module scope. /* or */ import has from 'has'; // https://www.npmjs.com/package/has // ... console.log(has.call(object, key));
Object.assign
來進行淺拷貝操做// very bad const original = { a: 1, b: 2 }; const copy = Object.assign(original, { c: 3 }); // this mutates `original` ಠ_ಠ delete copy.a; // so does this // 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 } // noA => { b: 2, c: 3 }
Function.prototype.call()
,調用一個函數,其具備指定的 this
值和參數列表。注意,該方法和 apply()
方法相似,區別在於 apply()
傳參爲一個包含多個參數的數組。可讓call()中的對象調用當前對象所擁有的function。
使用 call()
調用父構造函數,在一個子構造函數中,你能夠經過調用父構造函數的 call 方法來實現繼承,相似於Java中的寫法
//父構造函數,寫一些公用的方法和屬性 function a(v1,v2){ this.name = v1; this.cool = v2; } //子構造函數,能夠繼承父構造函數的方法和屬性,同時能夠有私有的方法和屬性 function b(v1,v2,v3){ a.call(this,v1,v2); this.sex = v3; } var v1 = new a('ylone',true); var v2 = new b('ylone',true,'male'); v1; ? {name: "ylone", cool: true} v2; ? {name: "ylone", cool: true, sex: "male"}
使用 call()
調用匿名函數,將參數做爲指定的 this值
,傳進匿名函數。同時也能夠傳遞普通參數。
var i = 1; (function(i){console.log(this,i)}).call(Math.random(),i); ? 0.9604319664333041 1
使用 call()
調用函數而且指定執行環境的this
function a(){ console.log(this.name + ' is ' + this.cool); }; var i = {name: 'ylone', cool: 'cool'}; a.call(i); ? ylone is cool
和 $.extend()
相似,用於對象的合併,將源對象內全部可枚舉的屬性拷貝到目標對象,注意若是源數據不是對象,則先會轉換成對象;若是是null
或者undefined
等不能轉換成對象的類型,則根據其位置進行跳過或者報錯。
Object.assign(null); ? Uncaught TypeError: Cannot convert undefined or null to object Object.assign(1,null); ? Number {1}
Object.assign()
僅支持淺拷貝,也就是說,若是源對象某個屬性的值是對象,那麼目標對象拷貝獲得的是這個對象的引用
var v1 = {a:{b:'b'}}; var v2 = Object.assign({},v1); v1.a.b = 'c'; v2.a.b; ? 'c'
Object.assign()
處理數組,會先把數組轉換成對象,將其視爲屬性名爲 0、一、2 的對象,所以源數組的 0 號屬性4覆蓋了目標數組的 0 號屬性1。
Object.assign([1, 2, 3], [4, 5]); ? Object.assign({0:1,1:2,2:3},{0:4,1:5}); ? {0:4,1:5,2:3} ? [4,5,3]
...
對象擴散運算符和對象剩餘運算符都用 ...
表示,能夠理解爲「脫衣服」方法
數組轉換,將數組轉換成逗號分隔的參數序列,注意,其返回值並非某個基本類型,因此該方法多用於函數參數設置,代替 apply()
方法。對於不少參數不能接受數組的方法提供了便利。
...[1,2,3] ? Uncaught SyntaxError: Unexpected number [...[1,2,3]] ? [1, 2, 3] [1,...[2,3],4] ? [1, 2, 3, 4] //Math.max()不支持數組傳參,以前經過apply()進行轉換 Math.max.apply(null,[1,2,3]) ? 3 //如今能夠利用 ... 直接進行轉換 Math.max(...[1,2,3]) ? 3
[]
來建立數組// bad const items = new Array(); // good const items = [];
push()
而不是直接給數組項賦值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];
...
將數組對象轉換爲數組const foo = document.querySelectorAll('.foo'); // good const nodes = Array.from(foo); // best const nodes = [...foo];
array.from()
而不是 ...
遍歷迭代器,這樣避免產生了中間變量// bad const baz = [...foo].map(bar); // good const baz = Array.from(foo, bar);
// good [1, 2, 3].map((x) => { const y = x + 1; return x * y; }); // good [1, 2, 3].map(x => x + 1); // bad - no returned value means `memo` becomes undefined after the first iteration [[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => { const flatten = memo.concat(item); memo[index] = flatten; }); // good [[0, 1], [2, 3], [4, 5]].reduce((memo, item, index) => { const flatten = memo.concat(item); memo[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, ];
Array.from()
方法從一個相似數組(一個對象必須有length屬性)或可迭代對象中建立一個新的數組實例,好比 array,map,set,string
//數組 const arr = ['1','2','3']; Array.from(arr); ? ["1", "2", "3"] //字符串 const str = 'ylone'; Array.from(str); ? ["y", "l", "o", "n", "e"] //map對象 const m1 = new Map(); m1.set('v1',1); m2.set('v2',2); m2; ? {"v1" => 1, "v2" => 2} Array.from(m2); ? [['v1',1],['v2',2]] //json對象 const j = {'v1':1,'v2':2}; j.length; ? undefined Array.from(j); ? []
Array.from(arrayLike, mapFn, thisArg)
arrayLike
表示想要轉換成數組的僞數組對象或可迭代對象mapFn(可選參數)
表示新數組中的每一個元素會執行該回調函數thisArg(可選參數)
表示執行回調函數mapFn
時this
對象Array.from([1,2,3], function(n){return n+1}) ? [2, 3, 4]
// 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;
// bad function processInput(input) { return [left, right, top, bottom]; } // the caller needs to think about the order of return data const [left, __, top] = processInput(input); // good function processInput(input) { return { left, right, top, bottom }; } // the caller selects only the data they need const { left, top } = processInput(input);
//數組解構 const arr = [1,[2,3],4]; const [a,[b,c],d] = arr; a,b,c,d; ? 1,2,3,4 //函數傳參 var arr = [1, 2, 3]; function fn1([a, b, c]) { return a+b+c; } fn1(arr); ? 6
''
// bad const name = "Capt. Janeway"; // bad - template literals should contain interpolation or newlines 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.';
// 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()
方法,由於它有潛在的危險,在不受信任的代碼上使用能夠打開一個程序多達幾種不一樣的注入攻擊\
,由於它影響可讀性,同時可能與轉義符產生影響// bad const foo = '\'this\' \i\s \"quoted\"'; // good const foo = '\'this\' is "quoted"'; const foo = `my name is '${name}'`;
// bad function foo() { // ... } // bad const foo = function () { // ... }; // good // lexical name distinguished from the variable-referenced invocation(s) const short = function longUniqueMoreDescriptiveLexicalFoo() { // ... };
()
建立的函數須要當即調用,自調用函數至關於一個獨立模塊。事實上,IIFE不多在項目中使用// immediately-invoked function expression (IIFE) (function () { console.log('Welcome to the Internet. Please follow me.'); }());
if
,while
等)裏面聲明一個函數。將函數分配給一個變量來替代它。由於雖然瀏覽器支持這種作法,可是他們各自的解析方式並不同// bad if (currentUser) { function test() { console.log('Nope.'); } } // good let test; if (currentUser) { test = () => { console.log('Yup.'); }; }
arguments
,這將致使每一個函數做用域的 arguments
對象被優先替換// bad function foo(name, options, arguments) { // ... } // good function foo(name, options, args) { // ... }
arguments
,而使用 ...
,由於 arguments
只是相似數組// 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) { // No! We shouldn’t mutate function arguments. // Double bad: if opts is falsy it'll be set to an object which may // be what you want but it can introduce subtle bugs. 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 = {}) { // ... }
Function
構造函數來建立一個新的函數,由於它和 eval()
狼狽爲奸// 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) { // ... }
...
來調用可變參數函數,由於 ...
很乾淨,不須要提供上下文環境,而且你不能輕易地使用 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, );
function(name){param1 = defaultValue1,...,paramN = defaultValueN}
JavaScript中函數的參數默認是 undefined
const a = function test(v1,v2=1){ return v1*v2; } a(5,5); ? 25 a(5); ? 5 a(void 0,5); ? NaN
undefined
,則會用默認參數替換,不然爲原傳參值有默認值的解構函數,經過解構賦值爲參數賦值
const b = function test([a,b]=[1,2],{c:c}={c:3}){ return a+b+c; } b(); ? 6 b([2,3],4); ? 9 b(void 0,4); ? 9 b([void 0,3],4); ? NaN
// 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聲明
// 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, })); // No implicit return with side effects 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, ) ));
// 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; };
const 函數名 = (參數...) => {函數聲明}||表達式
執行體爲函數聲明時須要加上 {}
,參數的規則參看上文內容
//支持解構函數 const f = ([a,b]=[1,2],{c:c}={c:3})=>a+b+c; f(); ? 6;
prototype
, 多用 class
。由於 class
語法更加簡潔和且閱讀性更棒// 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
實現繼承,由於這是繼承原型的內置功能// 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() luke.setHeight(20);
toString()
方法也沒問題,可是須要保證其能執行且沒有其餘影響class Jedi { constructor(options = {}) { this.name = options.name || 'no name'; } getName() { return this.name; } toString() { return `Jedi - ${this.getName()}`; } }
// 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; } }
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;
// 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';
// bad let foo = 3; export { foo }; // good const foo = 3; export { foo };
// bad export function foo() {} // good export default function foo() {}
// 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';
webpack
加載器語法。由於導入中使用加載器語法會將代碼耦合到模塊打包器中,,更建議使用 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
這些。使用 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);
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;
[]
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;
const
或者 let
來聲明變量,這樣作能夠避免污染全局命名空間// bad superPower = new SuperPower(); // good const superPower = new SuperPower();
const
或者 let
。這樣作,能夠獨立的聲明每個變量,而不須要考慮 ;
和,
的關係,同時也方便對每一個聲明進行調試,而不是跳過全部的聲明// 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';
let
和 const
進行分組,這樣加強代碼可讀性// 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;
const
和 let
是塊做用域而不是函數做用域// 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; }
// bad (function example() { // JavaScript interprets this as // let a = ( b = ( c = 1 ) ); // The let keyword only applies to variable a; variables b and c become // global variables. let a = b = c = 1; }()); console.log(a); // throws ReferenceError console.log(b); // 1 console.log(c); // 1 // good (function example() { let a = 1; let b = a; let c = a; }()); console.log(a); // throws ReferenceError console.log(b); // throws ReferenceError console.log(c); // throws ReferenceError // the same applies for `const`
不要使用一元遞增和遞減操做符(++,--),由於一元遞增和一元遞減可能受到分號插入的影響,而且可能致使應用中的值遞增或者遞減,而且不會報錯。使用 num += 1
相似的語句也更加有表現力,而且能夠避免預先遞增或者遞減從而致使程序發生意外
// bad const 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 const 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; ```
var
聲明被置於函數做用域的頂部,可是他們的賦值不是, const
和let
聲明會被置於一個新概念TDZ內。所以, typeof()
方法再也不安全// we know this wouldn’t work (assuming there // is no notDefined global variable) function example() { console.log(notDefined); // => throws a ReferenceError } // creating a variable declaration after you // reference the variable will work due to // variable hoisting. Note: the assignment // value of `true` is not hoisted. function example() { console.log(declaredButNotAssigned); // => undefined var declaredButNotAssigned = true; } // the interpreter is hoisting the variable // declaration to the top of the scope, // which means our example could be rewritten as: function example() { let declaredButNotAssigned; console.log(declaredButNotAssigned); // => undefined declaredButNotAssigned = true; } // using const and let function example() { console.log(declaredButNotAssigned); // => throws a ReferenceError console.log(typeof declaredButNotAssigned); // => throws a ReferenceError const declaredButNotAssigned = true; }
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'); }; } // the same is true when the function name // is the same as the variable name. 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'); } }
===
,!==
取代 ==
,!=
條件語句好比 if
會強制使用 ToBoolean
抽象方法來進行轉換,而且遵循如下規則:
Strings 轉換爲 false 若是是空字符串 ''
, 其他爲 true
if ([0] && []) { // true // an array (even an empty one) is an object, objects will evaluate to 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
和 default
使用 {}
來建立塊,好比let
, const
, function
, class
也是如此。由於在整個 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 {} } }
// bad const foo = maybe1 > maybe2 ? "bar" : value1 > value2 ? "baz" : null; // split into 2 separated ternary expressions const maybeNull = value1 > value2 ? 'baz' : null; // better const foo = maybe1 > maybe2 ? 'bar' : maybeNull; // best const foo = maybe1 > maybe2 ? 'bar' : maybeNull;
// 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;
**
,%
與 +
,-
,*
,/
,這樣代碼更加有可讀性,而且澄清了開發者的意圖// bad const foo = a && b < 0 || c > 0 || d + 1 === 0; // bad const bar = a ** b - 5 % d; // bad // one may be confused into thinking (a || b) && c 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; }
if else
, else
須要和 if
的 }
在同一行// bad if (test) { thing1(); thing2(); } else { thing3(); } // good if (test) { thing1(); thing2(); } else { thing3(); }
if else
語句內每一個代碼塊都用了 return
語句,那麼 else
語句就沒有必要,分紅多個 if
語句就好了// 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 // 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; }
// 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` 的前綴,好讓其餘開發者迅速明白你的意思。 `FIXME`表示這個問題須要弄清楚,`TODO`表示這個問題須要解決 - 使用 `// FIXME` 去註釋問題
class Calculator extends Abacus { constructor() { super(); // FIXME: shouldn’t use a global here total = 0; } } ```
// TODO
去註釋問題的解決方法class Calculator extends Abacus { constructor() { super(); // TODO: total should be configurable by an options param this.total = 0; } }
tab
去設置兩個空格// bad function foo() { ∙∙∙∙let name; } // bad function bar() { ∙let name; } // good function baz() { ∙∙let 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', });
()
不須要空格// 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;
// 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);
// 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;
// bad function bar() { console.log(foo); } // bad if (baz) { console.log(qux); } else { console.log(foo); } // bad class Foo { constructor(bar) { this.bar = bar; } } // 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 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.'));
// 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', };
有時須要附加的逗號,一是爲了在 git
上能保持一致,由於 git
在增減以後都會帶上逗號,二是一些像Babel這樣的轉譯器會自動刪除沒必要要的逗號,這意味着沒必要擔憂傳統瀏覽器中的逗號尾隨問題
// bad - git diff without trailing comma const hero = { firstName: 'Florence', - lastName: 'Nightingale' + lastName: 'Nightingale', + inventorOf: ['coxcomb chart', 'modern nursing'] }; // good - git diff with trailing comma 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 );
;
結尾,防止javascript的自動分號插入機制使整個程序報錯// bad - raises exception const luke = {} const leia = {} [luke, leia].forEach(jedi => jedi.father = 'vader') // bad - raises exception const reaction = "No! That's impossible!" (async function meanwhileOnTheFalcon(){ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0` // ... }()) // bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI! function foo() { return 'search your feelings, you know it to be foo' } // good const luke = {}; const leia = {}; [luke, leia].forEach((jedi) => { jedi.father = 'vader'; }); // good const reaction = "No! That's impossible!"; (async function meanwhileOnTheFalcon(){ // handle `leia`, `lando`, `chewie`, `r2`, `c3p0` // ... }()); // good function foo() { return 'search your feelings, you know it to be foo'; }
String
類型// => 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(); // isn’t guaranteed to return a string // good const totalScore = String(this.reviewScore);
Number
類型,用 Number
或者 parseInt
進行強制轉換,一般 parseInt
須要一個基數來解析字符串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 was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */ const val = inputValue >> 0;
2147483647 >> 0; // => 2147483647 2147483648 >> 0; // => -2147483648 2147483649 >> 0; // => -2147483647
Booleans
類型const age = 0; // bad const hasAge = new Boolean(age); // good const hasAge = Boolean(age); // best const hasAge = !!age;
// bad function q() { // ... } // good function query() { // ... }
// bad const OBJEcttsssss = {}; const this_is_my_object = {}; function c() {} // good const thisIsMyObject = {}; function thisIsMyFunction() {}
// 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', });
// 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); }; }
// 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
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 = [ // ... ]; // also good const httpRequests = [ // ... ]; // best import TextMessageContainer from './containers/TextMessageContainer'; // best const requests = [ // ... ];
getVal()
和 setVal()
這樣的方式// bad class Dragon { get age() { // ... } set age(value) { // ... } } // good class Dragon { getAge() { // ... } setAge(value) { // ... } }
isVal()
或者 hasVal()
這樣的形式// bad if (!dragon.age()) { return false; } // good if (!dragon.hasAge()) { return false; }
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]; } }
// bad $(this).trigger('listingUpdated', listing.id); // ... $(this).on('listingUpdated', (e, listingId) => { // do something with listingId }); // good $(this).trigger('listingUpdated', { listingId: listing.id }); // ... $(this).on('listingUpdated', (e, data) => { // do something with data.listingId });
$
來聲明一個承載jquery的元素// bad const sidebar = $('.sidebar'); // good const $sidebar = $('.sidebar'); // good const $sidebarBtn = $('.sidebar-btn');
// bad function setSidebar() { $('.sidebar').hide(); // ... $('.sidebar').css({ 'background-color': 'pink', }); } // good function setSidebar() { const $sidebar = $('.sidebar'); $sidebar.hide(); // ... $sidebar.css({ 'background-color': 'pink', }); }
$('.sidebar ul')
或者 父級 > 子級 $('.sidebar > ul')
find
// bad $('ul', '.sidebar').hide(); // bad $('.sidebar').find('ul').hide(); // good $('.sidebar ul').hide(); // good $('.sidebar > ul').hide(); // good $sidebar.find('ul').hide();
Number.isNaN
來代替全局的 isNaN
,由於全局的 isNaN
會強制將非數字類型轉換爲數字類型,任何強制轉換爲非數字的都會返回true// bad isNaN('1.2'); // false isNaN('1.2.3'); // true // good Number.isNaN('1.2.3'); // false Number.isNaN(Number('1.2.3')); // true
Number.isFinite
來代替全局的 isFinite
,由於全局的 isFinite
會強制將非數字類型轉換爲數字類型,任何強制轉換爲有限數字的結果都會返回true// bad isFinite('2e3'); // true // good Number.isFinite('2e3'); // false Number.isFinite(parseInt('2e3', 10)); // true