New features of ES7

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.