在這整理了一些經常使用的ES6的知識,但願可以幫助開發者更加了解和運用ES6javascript
ES6提出兩個新的聲明變量的命令 let,const,其中let徹底能夠取代var(二者語義相同) 注:var命令存在變量提高做用,let命令沒有這個問題
java
在let和const之間,建議優先使用const,尤爲在全局環境,不該該設置變量,只應設置常量。 const優於let的幾個緣由:node
靜態字符串一概使用單引號或反引號,不使用雙引號,動態字符串使用反引號 例:react
const a = 'foobar';
const b = `foo{a}bar`;
複製代碼
例:es6
const arr = [1,2,3,4];
// bad
const first = arr[0];
const second = arr[1];
// good
const [first,second] = arr;
複製代碼
例:編程
// bad
function getFullName (user) {
const firstName = user.firstName;
const lastName = user.lastName;
}
// good
function getFullName (obj) {
const {firstName,lastName} = obj;
}
// best
function getFullName ({firstName,lastName}) {
...
}
複製代碼
例:數組
// bad
function processInput (input) {
return [left,right,top,bottom];
}
// good
function processInput (input) {
return {left,right,top,bottom};
const {left,right} = processInput (input);
}
複製代碼
例:安全
// bad
const a = {k1:v1,k2:v2,};
const b = {
k1:v1,
k2:v2
};
// good
const a = {k1:v1,k2:v2};
const b = {
k1:v1,
k2:v2,
};
複製代碼
例:數據結構
// bad
const a = {};
a.x = 3;
// if reshape unavoidable
const a = {};
Object.assign (a,{x:3});
// good
const a = {x:null};
a.x = 3;
複製代碼
例:app
// bad
const obj = {
id:5,
name:'xiaolei',
};
obj[getKey('enabled')] = true;
// good
const obj = {
id:5,
name:'xiaolei',
[getKey('enabled')]:true,
};
複製代碼
上面的代碼中,對象obj的最後一個屬性名,須要計算獲得。這時最好利用屬性表達式,在新建obj的時候,將該屬性與其餘屬性定義在一塊兒,這樣,全部屬性就在一個地方定義了。
例:
var ref = 'some value';
// bad
const atom = {
ref:ref,
value:1,
addValue:function (value) {
return atom.value + value;
},
};
// good
const atom = {
ref, // 注意此處的寫法
value:1,
addValue:function (value) {
return atom.value + value;
},
};
複製代碼
例:
// bad
const len = items.length;
const itemsCopy = [];
let i;
for (i = 0; i < len; i++) {
itemsCopy[i] = items[i];
}
// good
const itemsCopy = [...items];
複製代碼
例:
const foo = document.querySelectorAll('foo');
const nodes = Array.from(foo);
複製代碼
例:
(() => {
console.log('welcome to the Internet');
})();
複製代碼
例:
// bad
[1,2,3].map(function (x) {
return x * x;
});
// good
[1,2,3].map((x) => {
return x * x;
});
// best
[1,2,3].map(x => x * x);
複製代碼
例:
// bad
const self = this;
const boundMethod = function (...params) {
return method.apply(self.params);
};
// acceptable
const boundMethod = method.bind(this);
// best
const boundMethod = (...params) => method.apply(this.params);
複製代碼
例:
// bad
function divide (a,b,option=false) {
...
};
// good
function divide (a,b,{option=false}) {
...
};
複製代碼
例:
// bad
function handles (opts) {
opts = opts || {};
};
// good
function handles (opts = {}) {};
複製代碼
例:
// bad
function concatenateAl () {
const args = Array.prototype.slice.call(arguements);
return args.join('');
};
// good
function concatenateAl (...args) {
return args.join('');
};
複製代碼
1.注意區分==Object==和==Map==,只有模擬現實世界的實體對象時,才使用Object。 2.若是隻是須要key:value的數據結構,使用Map結構,由於Map有內建的遍歷機制。 例:
let map = new Map(arr);
for (let key of map.keys()) {
console.log(key);
};
for (let value of map.values()) {
console.log(value);
};
for (let item of map.entries()) {
console.log(item[0],item[1]);
};
複製代碼
用class取代須要prototype的操做,由於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;
}
}
複製代碼
例:
// bad
const moduleA = require('moduleA');
const func1 = moduleA.func1;
const func2 = moduleA.func2;
// good
import {func1,func2} from 'moduleA';
複製代碼
例:
// commonJs寫法
var React = require('react');
var Breadcrumbs = React.createClass({
render () {
return <nav />; } }); module.exports = Breadcrumbs; // ES6寫法 import React from 'react'; class Breadcrumbs extends React.Component{ render () { return <nav />; } }; export default Breadcrumbs; 複製代碼
若是模塊只有一個輸出值,就使用export default,若是模塊有多個輸出值,就不使用export default。 export default 與普通的 export 不要同時使用
例:
// bad
import * as myObject from './importModule';
// goood
import myObject from './importModule';
複製代碼
例:
function makeStyleGuide () {};
export default makeStyleGuide;
複製代碼
例:
const StyleGuide () {
es6:{}
};
export default StyleGuide;
複製代碼