Javascript 简明教程

JavaScript - ECMAScript 2016

ECMAScript 2016 版本的 JavaScript 于 2016 年发布。之前旧版本的 JavaScript 按数字命名,例如 ES5 和 ES6。自 2016 年以来,这些版本以发布年份命名,例如 ECMAScript 2016、ECMAScript 17 等。让我们讨论添加到 ECMAScript 2016 中的功能。

The ECMAScript 2016 version of JavaScript was released in 2016. Previously the old versions of JavaScript are named by numbers for example ES5 and ES6. Since 2016 the versions are named by the year they are released for example ECMAScript 2016, ECMAScript 17, etc. Lets discuss the featues added in ECMAScript 2016.

New Features Added in ECMAScript 2016

以下是添加到 ECMAScript 2016 版本 JavaScript 中的新方法、功能等。

Here are the new methods, features, etc., added to the ECMAScript 2016 version of JavaScript.

  1. Array includes() method

  2. Exponentiation Operator (**)

  3. Exponentiation Assignment Operator (**=)

这里,我们详细解释了每项特性。

Here, we have explained each feature in detail.

JavaScript Array includes() Method

JavaScript 数组 includes() 方法用于检查数组中是否包含特定元素。

The JavaScript array includes() methods is used to check whether the array contains a particular element.

Syntax

arr.include(searchElement, fromIndex);

此处 arr 是原始数组,searchElement 是要从中进行搜索的元素。fromIndex 是一个可选参数,如果传递,则搜索将从 fromIndex 索引开始。

Here arr is the original array, the searchElement is to be search from. The fromIndex is an optional argument if passed, the searching will start from the fromIndex index.

Example

在下面的代码中,我们使用数组 includes() 方法来检查 watches 数组是否包含“Noise”品牌。

In the below code, we use the array includes() method to check whether the watches array contains the 'Noise' brand.

<html>
<body>
   <div id = "output">Does watches array include Noise?</div>
   <script>
      const watches = ["Titan", "Rolex", "Noise", "Fastrack", "Casio"];
      document.getElementById("output").innerHTML += watches.includes("Noise");
   </script>
</body>
</html>
Does watches array include Noise? true

Example

在下面的代码中,我们使用数组 includes() 方法来检查 subjects 数组中是否包含从索引 1 开始搜索的“Python”主题。

In the below code, we use the array includes() method to check whether the subjects array contains the 'Python' subject searching from index 1.

<html>
<body>
   <div id = "output">Does subjects array include Python fromIndex 1? </div>
   <script>
      const subjects = ["Java", "JavaScript", "Python", "C", "C++"];
      document.getElementById("output").innerHTML += subjects.includes("Python", 1);
   </script>
</body>
</html>
Does subjects array include Python fromIndex 1? true

JavaScript Exponentiation Operator

JavaScript 求幂运算符用于求第一个操作数乘以第二个操作数的幂。

The JavaScript exponentiation operator is used to find the power of the first operand raised to the second operand.

Syntax

求幂运算符的语法如下:

The syntax of expponentiation operator is as follow −

x ** y;

它返回将第一个操作数 (x) 乘以第二个操作数 (y) 的幂的结果。

It returns the result of raising the first operand (x) to the power of the second operand (y).

Example

在下面的代码中,我们使用求幂运算符求 22 次方,并将结果值存储在“res”变量中。

In the below code, we find the 22 using the exponentiation operator and store the resultant value in the 'res' variable.

<html>
<body>
   <div id = "output">The resultant value for 2 ** 2 is: </div>
   <script>
      document.getElementById("output").innerHTML += 2 ** 2;
   </script>
</body>
</html>
The resultant value for 2 ** 2 is: 4

Exponentiation Assignment Operator

JavaScript 求幂赋值运算符将第一个操作数乘以第二个操作数的幂并将其赋值给第一个操作数。

The JavaScript exponentiation assignment operator raises the power of the first operand by the second operand and assigns it to the first operand.

Syntax

求幂赋值运算符的语法如下:

The syntax of exponentiation assignment operator is as follows −

x **= y;

它将第一个操作数 (x) 乘以第二个操作数 (y) 的幂的结果赋值给 x。

It assigns the result of raising the first operand (x) to the power of the second operand (y) to x.

Example

在下面的代码中,我们使用求幂赋值运算符求 102 次方并将结果值赋值给“num”变量。

In the below code, we find the 102 and assign the resultant value to the 'num' variable using the exponentiation assignment operator.

<html>
<body>
   <div id = "output">The resultant value for 10 ** 2 is: </div>
   <script>
      let num = 10;
      num **= 2; // exponentiation assignment operation
      document.getElementById("output").innerHTML += num;
    </script>
</body>
</html>
The resultant value for 10 ** 2 is: 100

ECMAScript 2016 Browser Support

大多数现代浏览器都支持 JavaScript 的 ECMAScript 2016 版本

Most modern browsers support the ECMAScript 2016 version of JavaScript

Chrome

Firefox

Microsoft Edge

Opera

Safari

Firefox Android

Yes

Yes

Yes

Yes

Yes

Yes