1. 使用解構(gòu)賦值減少代碼量
// es5
let detail = this.data.detail,
score = this.data.score,
totalScore = this.data.totalScore,
ratio = score / totalScore;
// es6解構(gòu)賦值
let { detail, score, totalScore } = this.data,
ratio = score / totalScore;
// 還可以使用別名
let { detail, score: score1, totalScore: score2 } = this.data,
ratio = score1 / score2;
// 篩選掉特定的鍵值
let obj = {a:1, b:2, c:3, d:4};
let {a, ...newObj} = obj;
console.log(newObj); // {b:2, c:3, d:4}
2. 同理燎猛,使用展開運算符減少代碼量
// es5
obj.a = obj2.a;
obj.b = obj2.b;
obj.c = c;
// es6展開運算符
obj = { ...obj, a: obj2.a, b: obj2.b, c};
// 注意恋捆,后面的同名屬性會覆蓋前面的,但不能用于深拷貝
3. Reflect
附ES5中 Object的一些特性
4. iterator
Array重绷、Map沸停、Set、arguments昭卓、nodeList愤钾、String等默認部署了 iterator,可以使用 for...of 進行遍歷候醒,若想給 Object 類型部署 iterator能颁,可以自定義 iterator 方法,若是偽數(shù)組則可以如下:
let iterable = {
0: 0,
1: 1,
length: 2,
[Symbol.iterator]: Array.prototype[Symbol.iterator]
};
for(let value of iterable) {
console.log(value); // 輸出 0 1
}
5. for...of循環(huán)的優(yōu)點
for 語法比較繁瑣
for...in 無法保證順序,無法中途使用return或break跳出,key會默認轉(zhuǎn)化為String類型漫玄,會遍歷原型鏈的鍵值
for...of 有著與for...in一樣簡潔的語法,但沒有那些缺點(經(jīng)過驗證镜硕,也會遍歷原型鏈的鍵值)
6. Array新特性
indexOf
every
some
reduce
isArray