在这整理了一些经常使用的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;
复制代码