ES2019指南

ESNext is a name that always indicates the next version of JavaScript.

ESNext是一个始终指示JavaScript的下一版本的名称。

The current ECMAScript version is ES2018. It was released in June 2018.

当前的ECMAScript版本是ES2018 。 它于2018年6月发布。

Historically JavaScript editions have been standardized during the summer, so we can expect ECMAScript 2019 to be released in summer 2019.

历史上JavaScript版本在夏季已标准化,因此我们可以预期ECMAScript 2019将于2019年夏季发布。

So at the time of writing, ES2018 has been released, and ESNext is ES2019

所以在撰写本文时,ES2018已发布,而ESNext是ES2019

Proposals to the ECMAScript standard are organized in stages. Stages 1-3 are an incubator of new features, and features reaching Stage 4 are finalized as part of the new standard.

ECMAScript标准的提案分阶段进行。 阶段1-3是新功能的孵化器,达到阶段4的功能将作为新标准的一部分最终确定。

At the time of writing we have a number of features at Stage 4. I will introduce them in this section. The latest versions of the major browsers should already implement most of those.

在撰写本文时,我们在第4阶段具有许多功能。 我将在本节中介绍它们。 主要浏览器的最新版本应该已经实现了其中的大多数。

  • Array.prototype.{flat,flatMap}

    Array.prototype.{flat,flatMap}

  • Optional catch binding

    可选的捕获绑定
  • Object.fromEntries()

    Object.fromEntries()

  • String.prototype.{trimStart,trimEnd}

    String.prototype.{trimStart,trimEnd}

  • Symbol.prototype.description

    Symbol.prototype.description

  • JSON improvements

    JSON改进
  • Well-formed JSON.stringify()

    JSON.stringify()

  • Function.prototype.toString()

    Function.prototype.toString()

Some of those changes are mostly for internal use, but it’s also good to know what is going on.

其中一些更改主要供内部使用,但也知道发生了什么也很好。

There are other features at Stage 3, which might be promoted to Stage 4 in the next few months, and you can check them out on this GitHub repository: https://github.com/tc39/proposals.

第3阶段还有其他功能,可能会在接下来的几个月中升级到第4阶段,您可以在以下GitHub存储库中查看它们: https : //github.com/tc39/proposals

Array.prototype.{flat,flatMap} (Array.prototype.{flat,flatMap})

flat() is a new array instance method that can create a one-dimensional array from a multidimensional array.

flat()是一种新的数组实例方法,可以从多维数组创建一维数组。

Example:

例:

['Dog', ['Sheep', 'Wolf']].flat()
//[ 'Dog', 'Sheep', 'Wolf' ]

By default it only “flats” up to one level, but you can add a parameter to set the number of levels you want to flat the array to. Set it to Infinity to have unlimited levels:

默认情况下,它仅“展平”到一个级别,但是您可以添加一个参数来设置要将阵列展平到的级别数。 将其设置为Infinity以具有无限级别:

['Dog', ['Sheep', ['Wolf']]].flat()
//[ 'Dog', 'Sheep', [ 'Wolf' ] ]

['Dog', ['Sheep', ['Wolf']]].flat(2)
//[ 'Dog', 'Sheep', 'Wolf' ]

['Dog', ['Sheep', ['Wolf']]].flat(Infinity)
//[ 'Dog', 'Sheep', 'Wolf' ]

If you are familiar with the JavaScript map() method of an array, you know that using it you can execute a function on every element of an array.

如果您熟悉数组JavaScript map()方法,那么您就会知道,使用它可以对数组的每个元素执行一个函数。

flatMap() is a new Array instance method that combines flat() with map(). It’s useful when calling a function that returns an array in the map() callback, but you want your resulted array to be flat:

flatMap()是一个新的Array实例方法,将flat()map()结合在一起。 在调用返回map()回调中的数组的函数时非常有用,但是您希望得到的数组是平坦的:

['My dog', 'is awesome'].map(words => words.split(' '))
//[ [ 'My', 'dog' ], [ 'is', 'awesome' ] ]

['My dog', 'is awesome'].flatMap(words => words.split(' '))
//[ 'My', 'dog', 'is', 'awesome' ]

可选的捕获绑定 (Optional catch binding)

Sometimes we dont need to have a parameter binded to the catch block of a try/catch.

有时我们不需要将参数绑定到try / catch的catch块。

We previously had to do:

我们以前不得不做:

try {
  //...
} catch (e) {
  //handle error
}

Even if we never had to use e to analyze the error. We can now simply omit it:

即使我们永远不必使用e来分析错误。 现在我们可以简单地忽略它:

try {
  //...
} catch {
  //handle error
}

Object.fromEntries() (Object.fromEntries())

Objects have an entries() method, since ES2017.

ES2017开始 ,对象具有entries()方法。

It returns an array containing all the object own properties, as an array of [key, value] pairs:

它返回一个包含所有对象自身属性的数组,作为[key, value]对的数组:

const person = { name: 'Fred', age: 87 }
Object.entries(person) // [['name', 'Fred'], ['age', 87]]

ES2019 introduces a new Object.fromEntries() method, which can create a new object from such array of properties:

ES2019引入了一个新的Object.fromEntries()方法,该方法可以通过以下属性数组创建一个新对象:

const person = { name: 'Fred', age: 87 }
const entries = Object.entries(person)
const newPerson = Object.fromEntries(entries)

person !== newPerson //true

String.prototype.{trimStart,trimEnd} (String.prototype.{trimStart,trimEnd})

This feature has been part of v8/Chrome for almost a year now, and it’s going to be standardized in ES2019.

此功能已经成为v8 / Chrome的一部分了将近一年,它将在ES2019中进行标准化。

trimStart() (trimStart())

Return a new string with removed white space from the start of the original string

从原始字符串的开头返回删除了空白的新字符串

'Testing'.trimStart() //'Testing'
' Testing'.trimStart() //'Testing'
' Testing '.trimStart() //'Testing '
'Testing '.trimStart() //'Testing'

trimEnd() (trimEnd())

Return a new string with removed white space from the end of the original string

返回一个新字符串,该字符串从原始字符串的末尾删除了空白

'Testing'.trimEnd() //'Testing'
' Testing'.trimEnd() //' Testing'
' Testing '.trimEnd() //' Testing'
'Testing '.trimEnd() //'Testing'

Symbol.prototype.description (Symbol.prototype.description)

You can now retrieve the description of a Symbol by accessing its description property instead of having to use the toString() method:

现在,您可以通过访问符号description属性来检索符号description而不必使用toString()方法:

const testSymbol = Symbol('Test')
testSymbol.description // 'Test'

JSON改进 (JSON improvements)

Before this change, the line separator (\u2028) and paragraph separator (\u2029) symbols were not allowed in strings parsed as JSON.

进行此更改之前,在解析为JSON的字符串中不允许使用行分隔符(\ u2028)和段落分隔符(\ u2029)。

Using JSON.parse(), those characters resulted in a SyntaxError but now they parse correctly, as defined by the JSON standard.

使用JSON.parse() ,这些字符会导致SyntaxError但现在它们可以正确解析,如JSON标准所定义。

JSON.stringify() (Well-formed JSON.stringify())

Fixes the JSON.stringify() output when it processes surrogate UTF-8 code points (U+D800 to U+DFFF).

修复处理代理UTF-8代码点(U + D800至U + DFFF)时的JSON.stringify()输出。

Before this change calling JSON.stringify() would return a malformed Unicode character (a “�”).

在此更改之前,调用JSON.stringify()将返回格式错误的Unicode字符(“。”)。

Now those surrogate code points can be safely represented as strings using JSON.stringify(), and transformed back into their original representation using JSON.parse().

现在,可以使用JSON.stringify()将这些代理代码点安全地表示为字符串,并使用JSON.parse()将其转换回其原始表示形式。

Function.prototype.toString() (Function.prototype.toString())

Functions have always had an instance method called toString() which return a string containing the function code.

函数始终有一个名为toString()的实例方法,该方法返回包含函数代码的字符串。

ES2019 introduced a change to the return value to avoid stripping comments and other characters like whitespace, exactly representing the function as it was defined.

ES2019引入了对返回值的更改,以避免剥离注释和其他字符(如空格),以准确地代表所定义的函数。

If previously we had:

如果以前我们有:

function /* this is bar */ bar () {}

The behavior was this:

行为是这样的:

bar.toString() //'function bar() {}

now the new behavior is:

现在的新行为是:

bar.toString(); // 'function /* this is bar */ bar () {}'

翻译自: https://flaviocopes.com/es2019/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值