ecmascript6介绍
by Sanket Meghani
通过Sanket Meghani
介绍ECMAScript 2016(ES7)添加到JavaScript的新功能 (Introducing the new features that ECMAScript 2016 (ES7) adds to JavaScript)
Since ECMAScript 2015 (also known as ES6) was released, it has introduced a huge set of new features. They include arrow functions, sets, maps, classes and destructuring, and much more. In many ways, ES2015 is almost like learning a new version of JavaScript.
自ECMAScript 2015(也称为ES6)发布以来,它引入了许多新功能。 它们包括箭头函数,集合,地图,类和解构等等。 在许多方面,ES2015几乎就像学习JavaScript的新版本一样。
Ecma Technical Committee 39 governs the ECMA specification. They decided to release a new version of ECMAScript every year starting in 2015. A yearly update means no more big releases like ES6.
Ecma技术委员会39管理ECMA规范。 他们决定从2015年开始每年发布ECMAScript的新版本。每年更新意味着不再有像ES6这样的大版本。
ECMAScript 2016 introduced only two new features:
ECMAScript 2016仅引入了两个新功能:
- Array.prototype.includes() Array.prototype.includes()
- Exponentiation operator 求幂运算符
Array.prototype.includes() (Array.prototype.includes())
Array.prototype.includes()
checks the array for the value
passed as an argument
. It returns true
if the array contains the value
, otherwise, it returns false
.
Array.prototype.includes()
检查数组中作为argument
传递的value
。 它返回true
如果数组包含的value
,否则,返回false
。
Before, we needed to use Array.prototype.indexOf()
to check if the given array contains an element or not.
在此之前,我们需要使用Array.prototype.indexOf()
来检查给定的数组是否包含一个元素。
let numbers = [1, 2, 3, 4];
if(numbers.indexOf(2) !== -1) { console.log('Array contains value');}
With ECMA2016, we can write:
使用ECMA2016,我们可以编写:
if(numbers.includes(2)) { console.log('Array contains value');}
Array.prototype.includes()
handles NaN
better than Array.prototype.indexOf()
. If the array contains NaN
, then indexOf()
does not return a correct index while searching for NaN
.
Array.prototype.includes()
处理NaN
比Array.prototype.indexOf()
更好。 如果数组包含NaN
,则indexOf()
在搜索NaN
时不会返回正确的索引。
Array.prototype.includes()
returns the correct value when searching for NaN
.
搜索NaN
时, Array.prototype.includes()
返回正确的值。
NaN
is a property of the JavaScript global object and represents a value that is Not-a-Number. There are known quirks when comparing NaN
to another value. These are addressed in Array.prototype.includes()
, but not in Array.protoype.indexOf
.
NaN
是JavaScript全局对象的属性,表示一个非数字值。 将NaN
与另一个值进行比较时,存在一些已知的怪癖。 这些在Array.prototype.includes()
中解决,但不在Array.protoype.indexOf
。
let numbers = [1, 2, 3, 4, NaN];
console.log(numbers.indexOf(NaN)); //Prints -1console.log(numbers.includes(NaN)); //Prints true
求幂运算符 (Exponentiation Operator)
JavaScript already supports many arithmetic operators like +, -, *
and more.
JavaScript已经支持许多算术运算符,例如+, -, *
等。
ECMAScript 2016 introduced the exponentiation operator, **
.
ECMAScript 2016引入了幂运算符**
。
It has the same purpose as Math.pow()
. It returns the first argument raised to the power of the second argument.
它具有与Math.pow()
相同的目的。 它将第一个自变量返回第二个自变量的幂。
let base = 3;let exponent = 4;let result = base**exponent;
console.log(result); //81
结论 (Conclusion)
New features introduced by ECMA2016 provide convenient alternatives to existing functionalities.
ECMA2016引入的新功能为现有功能提供了方便的替代方法。
Looking ahead, ECMA2017 was finalized in June of this year. New features include async/await
, SharedArrayBuffer
and some useful methods to Object.prototype
.
展望未来,ECMA2017已于今年6月完成。 新功能包括async/await
, SharedArrayBuffer
和一些有用的方法来Object.prototype
。
翻译自: https://www.freecodecamp.org/news/ecmascript-2016-es7-features-86903c5cab70/
ecmascript6介绍