New features of ES7 ES7 features:
1.Array.prototype.includes
2.Exponentiation Operator (exponentiation operation)
One, Array.prototype.includes
Array.prototype.includes usage is easy and simple. It is an alternative to indexof, a developer used to check for the presence of values in an array, and indexof is an awkward use because it returns an element in the array position or 1 when such an element cannot be found. So it returns a number instead of a Boolean value. Developers need to implement additional checks. In ES6, to check if there are values you need to do some of the following tips as they do not match to values, Array.prototype.indexOf returns 1 to True (converted to true), but when the matched element is a 0 position, the array contains the element, but it becomes false. Includes checks whether a value exists in an array or in a list.
Let arr = [' react ', ' angular ', ' vue ']//wrongif (arr.indexof (' react ')) {//0-evaluates to False, definitely as we E xpected console.log (' Can use React ')//This line would never be executed}//correctif (arr.indexof (' React ')!==-1) { console.log (' Can use React ')}
Use a little bit of the hack bitwise operator to ~
make the code more compact because ~
(bit XOR) is equivalent to any number -(a + 1)
:
Let arr = [' react ', ' angular ', ' vue ']//correctif (~arr.indexof (' react ') ") { console.log (' Can use react ')}
The following code is used in ES7 includes
:
Let arr = [' react ', ' angular ', ' vue ']//correctif (arr.includes (' react ') ") { console.log (' Can use react ')}
You can also use the code in the string includes
as follows:
Let str = ' react-native '//Correctif (Str.tolowercase (). Includes (' React ')) { //True console.log (' Found ' React "') }
Two, exponentiation Operator (exponentiation operation) * *
Exponentiation is mostly a mathematical calculation for developers, which is useful for 3d,vr,svg and data visualization. In ES6 or earlier, you have to create a loop, create a recursive function, or useMath.pow,在ES6/2015ES,你能使用Math.pow
创建一个短的递归箭头函数
Calculateexponent = (base, exponent) = base* ((--exponent>1)? Calculateexponent (base, exponent): base) Console.log (calculateexponent (7,2) = = = Math.pow (7,2))//Trueconsole.log (calculateexponent (2,7) = = = Math.pow (2,7))// True
In es7/es2016, a developer with a math wizard can use the shorter syntax:
Let A = 7 * * 2let B = 2 * * 7console.log (A = = Math.pow (7,2))//Trueconsole.log (b = = = Math.pow (2,7))//True
New features of ES7