Skip to main content

ES2016

Free2018-10-20#JS#es2016特性#es7新特性#es规范发布流程#js指数运算符#js幂运算符

An array inclusion detection method and an exponentiation operator. That's it.

1. Feature Overview

ES2016 (also known as ES7) was released in June 2016 and contains only 2 new features:

An array inclusion detection method and an exponentiation operator. That's it. ES2016 has only these two new features, and they are both just icing on the cake.

P.S. For more approved features, see Finished Proposals

2. Array.prototype.includes

The includes method for arrays is as follows:

// 返回true | false,表示包含不包含
// 可选参数fromIndex表示从该index开始找
//    默认是0,负数表示倒数第几个(array.length + fromIndex)
array.includes(item, [fromIndex])

It is used to determine whether an array contains a specified element, similar to (but not equivalent to):

array.indexOf(item) !== -1

There are subtle differences between the two:

  • NaN: [NaN].includes(NaN) === true while [NaN].indexOf(NaN) === -1

  • Sparse arrays: [1, , 3].includes(undefined) === true while [1, , 3].indexOf(undefined) === -1

In short, includes can handle NaN and will not skip empty slots in sparse arrays (that is, it doesn't check if the elements in the array exist).

Specifically, includes uses the SameValueZero algorithm when comparing equality:

  • Objects are compared by reference only.

  • Primitive values are compared by type and value.

  • There are 2 special cases in value comparison: +0 and -0 are equal, and NaN and NaN are equal.

3. Exponentiation operator

The exponentiation operator is as follows:

// 底数 ** 指数
base ** exponent

In addition, **= is also provided:

let n = 2;
// 运算并赋值
n **= 4;
n; // => 16

It is completely equivalent to Math.pow(base, exponent), including these special cases:

NaN ** 0;       // => 1
Infinity ** 0;  // => 1
-5 ** NaN;      // => NaN
NaN ** NaN;     // => NaN

That's right, completely equivalent, as exponentiation cannot be redefined (any inconsistency would cause confusion).

4. Why is this version so small?

This is related to the ES release process. ES feature proposals are divided into 5 stages:

  • stage 0 (Strawman): Initial draft (rougher than a Draft)

  • stage 1 (Proposal): Proposal

  • stage 2 (Draft): Draft

  • stage 3 (Candidate): Candidate proposal

  • stage 4 (Finished): Approved proposal

Feature proposals can be submitted at any time, with multi-line parallel reviews and advancement, but the release cycle is fixed:

  • February 1: Produce the Candidate Draft

  • February-March: 60-day royalty-free opt-out period

  • March TC39 meeting: Merge stage 4 (approved) proposals, calibrate final semantics, and branch the new specification version from the main branch, after which only editorial changes are accepted.

  • April-June: ECMA CC and ECMA GA review period

  • July: The ECMA General Assembly approves the new standard

A new version is released every July, carrying the newly approved feature proposals. Therefore, the number of new features included in a version depends on how many features were approved that year.

This ensures that all features released in the specification have been extensively reviewed and confirmed, and on the other hand, maintains a frequent annual release rhythm, avoiding major versions like ES2015 (which took 6 years to produce).

P.S. For more information on the Ecma TC39 ES release process, see The TC39 Process

References

Comments

No comments yet. Be the first to share your thoughts.

Leave a comment