不知道小夥伴們ES6的特性學的怎麼樣了?ES2016(ES7)和ES2017(ES8)都已經要出來了,本文爲你們整理介紹一下ES7的新特性。html
ES7特性只有兩個:數組
Array.prototype.includes
**
Array.prototype.includes
Array.prototype.includes(value:任意值): boolean
bash
includes()
方法用來判斷一個數組是否包含一個指定的值,根據狀況,若是包含則返回 true,不然返回false。ui
let a = [1, 2, 3];
a.includes(2); // true
a.includes(4); // false
複製代碼
語法:this
arr.includes(searchElement)
arr.includes(searchElement, fromIndex)
複製代碼
看到這裏你們會感受和indexOf方法很像,spa
[1, 2, 3].includes(1) // true
[1, 2, 3].indexOf(1) // 0
複製代碼
可是有一點和indexOf不同,includes()
方法能找到 NaN,而 indexOf()
不行:prototype
[NaN].includes(NaN) //true
[NaN].indexOf(NaN) //-1
複製代碼
接下來咱們看下polyfilcode
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
var len = o.length >>> 0;
if (len === 0) {
return false;
}
var n = fromIndex | 0;
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
while (k < len) {
if (o[k] === searchElement) {
return true;
}
k++;
}
return false;
}
});
}
複製代碼
能夠看到includes()
方法的實現,就是用while循環數組,若是找到了searchElement便返回true
,不然返回false
。htm
**
在ES6,若是你想作一些求冪運算的話,你須要用到Math.pow(x, y)
方法,blog
如今在ES7 /ES2016,以數學嚮導的開發者能夠使用更短的語法:
let a = 7 ** 12
let b = 2 ** 7
console.log(a === Math.pow(7,12)) // true
console.log(b === Math.pow(2,7)) // true
複製代碼
**
是一種運算符,就像+
,-
,*
,/
同樣,還能夠
let a = 7
a **= 12
let b = 2
b **= 7
console.log(a === Math.pow(7,12)) // true
console.log(b === Math.pow(2,7)) // true
複製代碼
參考閱讀: