ES2019 features (ES10)

Tram Ho

Array.prototype. {Flat, flatMap}

flat() is a new instance method of array, it can create one-dimensional array from a multi-dimensional array.

For example:

By default the function will only “flat” one way, you can add param to set the number of dimensions you want. You can set infinity to flat all dimensions:

You must be familiar with the array map() method of arrays. As you know, it is used to execute a certain function on each element of the array.

flatMap() is a new instance method of an associative array between flat() and map() . This is useful when running a function that returns an array but wants the array to be flat:

Optional catch

Previously, when using try/catch you always had to write:

even if you don’t need to use e to handle errors. Now you can remove it if you don’t need it:

Object.fromEntries ()

Since ES2017, Object has more method entries() . It returns an array containing all the properties of the object, the return array will be in the form of key and value pairs:

ES2019 brings a new method fromEntries() , used to create a new object from an array of properties:

String.prototype. {TrimStart, trimEnd}

This feature appeared in v8 / Chrome and later it was also standardized in ES2019.

trimStart()

Returns a string whose whitespace has been removed at the beginning of the original string:

trimEnd()

Returns a string with spaces removed at the end of the original string:

Symbol.prototype.description

You can now get a description of a symbol by accessing the description property instead of using the toString() method:

Improve JSON

Before being improved, if a string contained line characters ( u2028) and text segments ( u2029), when using JSON.parse() , those characters would be reported as SyntaxError . Now they can be normal parse.

Fix JSON.stringify () error

Fix JSON.stringify() output error when handling UTF-8 code points instead (from U + D800 to U + DFFF).

Previously, when calling JSON.stringify() with alternate code points, it returned an error Unicode character (the “�” character).

Now, those alternative code points can now be represented as strings in a normal way with JSON.stringify() , and can be converted back to their original form using JSON.parse() .

Function.prototype.toString ()

The function has an instance method called toString() returns a string containing the function’s code.

ES2019 changed the return value of this method to avoid removing comments and other characters (such as whitespace), representing the exact code of the declared function.

Previously, the function was declared as follows:

The result will look like this:

Now the result will look like this:


(Over)

By the time this article is completed, ES2019 is the latest version of ECMAScript, so I would like to pause the series here. I will try to update when new features are released, thank you for watching in the past. ?

Share the news now

Source : Viblo