SoFunction
Updated on 2025-03-01

JavaScript: New features of ES2019 (translation)

As one of the most popular programming languages ​​and one of the most important web development languages, JavaScript continues to evolve, and each iteration receives some new internal updates. Let's take a look at what new features are available in ES2019 and add them to our daily development:

()

() Recursively flip the nested array to the specified depth. The default value is 1, and if you want full depth, use Infinity. This method does not modify the original array, but creates a new array:

const arr1 = [1, 2, [3, 4]];
(); 
// [1, 2, 3, 4]

const arr2 = [1, 2, [3, 4, [5, 6]]];
(2); 
// [1, 2, 3, 4, 5, 6]

const arr3 = [1, 2, [3, 4, [5, 6, [7, 8]]]];
(Infinity); 
// [1, 2, 3, 4, 5, 6, 7, 8]

The flat() method removes empty items in the array:

const arr4 = [1, 2, , 4, 5];
(); // [1, 2, 4, 5]

()

The flatMap() method first maps each element using a mapping function, and then compresses the result into a new array. It is almost the same as , and , with depth values ​​of 1 , but flatMap is usually slightly more efficient in merging into one method.

const arr1 = [1, 2, 3];

(x => [x * 4]); 
// [[4], [8], [12]]

(x => [x * 4]); 
// [4, 8, 12]

Better examples:

const sentence = ["This is a", "regular", "sentence"];

(x => (" ")); 
// [["This","is","a"],["regular"],["sentence"]]

(x => (" ")); 
// ["This","is","a","regular", "sentence"]

// You can use reduce and merge to achieve the same functions.((acc, x) => ((" ")), []);

() and ()

In addition to () which can delete whitespace characters from both ends of a string, there is now a separate method that can only delete spaces from each end:

const test = " hello ";

(); // "hello";
(); // "hello ";
(); // " hello";
  • trimStart(): alias trimLeft(), removes the continuous whitespace at the left end of the original string and returns, and does not directly modify the original string itself.
  • trimEnd(): alias trimRight(), removes the continuous whitespace at the right end of the original string and returns, and does not directly modify the original string itself.


A new way to convert a list of key-value pairs to an Object.

It is exactly the opposite of the existing () method, which is used when converting an object into an array, returns an array of key-value pairs of the given object itself enumerable properties.

But now you can return the array of operations to the object by .

Here is an example (square the values ​​of all object properties):

const obj = { prop1: 2, prop2: 10, prop3: 15 };

// Convert to key-value pair array:let array = (obj); 
// [["prop1", 2], ["prop2", 10], ["prop3", 15]]

Square the values ​​of all object properties:

array = (([key, value]) => [key, (value, 2)]); 
// [["prop1", 4], ["prop2", 100], ["prop3", 225]]

We pass the converted array array as a parameter to convert the array into an object:

const newObj = (array); 
// {prop1: 4, prop2: 100, prop3: 225}

Optional Catch parameters

The new proposal allows you to omit the catch() parameter completely, because in many cases you don't want to use it:

try {
 //...
} catch (er) {
 //handle error with parameter er
}

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


description is a read-only property that returns a string of optional descriptions of the Symbol object, used instead of the toString() method.

const testSymbol = Symbol("Desc");

; // "Desc"

(); // "Symbol(Desc)"

()

Calling toString() on the function now returns the function, exactly the same as its definition, including spaces and comments.

Before:

function /* foo comment */ foo() {}

(); // "function foo() {}"

Now:

(); // "function /* foo comment */ foo() {}"

() Improvement

Line delimiters (u2028) and paragraph delimiters (u2029) are now parsed correctly instead of reporting a syntax error.

var str = '{"name":"Bottle\u2028AnGe"}'
(str)
// {name: "Bottle
AnGe"}

Original link:JavaScript: What's new in ES2019

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.