
ES2016(ES7)新屬性
一、Array.prototype.includes(ele, index)
確定數(shù)組中是否存在某個元素,存在返回true,不存在返回false。
ele -> 元素(可選)
index -> 元素的位置(可選)
introduction
- (1)解決判斷數(shù)組中是否存在某個元素更加語義化
old
if(arr.indeOf(ele) !== -1) {
// some code
}
// or
if(~arr.indexOf) {
// some code
}
now
arr.includes(ele) // true or false
- (2)如果數(shù)組中存在NaN,indexOf無法找到,必須寫hack
const arr = [NaN];
arr.indexOf(NaN); // -1
arr.includes(NaN); // true
二、**運(yùn)算符
冪運(yùn)算符
a ** b
introduction
- 簡單易讀
old
// Math.pow(a, b)
Math.pow(2, 3) // 8
now
2**3 // 8