Just four hours ago, TC39 added the following features to ES2019. Let’s take a look at how these new features change things for us.
New ES2019 features:
➡ ️ Array# {flat, flatMap}
➡ ️ Object. FromEntries
➡ ️ String# {trimStart, trimEnd}
➡ ️ Symbol# description
➡️ try {} catch {} // optional binding
➡ ️ JSON ⊂ ECMAScript
➡ ️ well – formed JSON. Stringify
➡ ️ stable Array# sort
➡ ️ revised Function# toString
JSON ⊂ ECMAScript (JSON Superset)
Line delimiter (U + 2028) and segment delimiter (U + 2029) symbols are now allowed to match JSON within string literals. Previously, these symbols were treated as line terminators in string literals, so their use would result in a SyntaxError exception.
well-formed JSON.stringify
More friendly json. stringify (fixed bug for displaying some Out-of-range Unicode.)
If you enter a character in Unicode format but out of range, the original json. stringify returns the Unicode string in the wrong format:
JSON.stringify('\uD800'); / / -'" �"
Copy the code
A phase 3 proposal that changes json.stringify is now implemented, so it outputs an escape sequence for it, making it valid Unicode (and represented as UTF-8) :
JSON.stringify('\uD800'); / / -'"\\ud800"'The output in the Chrome console is'"\ud800"', but is only a display value, the real value is'"\\ud800"'. JSON.stringify('\uD800') = = ='"\\ud800"'; / / -true
Copy the code
stable Array#sort
Previously, in sort, V8 used the unstable QuickSort for arrays of more than 10 elements. Now, use the stable TimSort algorithm.)
TimSort algorithm: en.wikipedia.org/wiki/Timsor…
revised Function#toString
Function. The prototype. The toString () now returns precise characters, including Spaces and comments. Comparison of the former and the present:
// Note the comment between the `function` keyword
// and the function name, as well as the space following
// the function name.
function /* a comment */ foo() {} // Previously: foo.toString(); / / -'function foo() {}'// ^ no comment // ^ no space // Now: foo.toString(); / / -'function /* comment */ foo () {}'
Copy the code
Array #{flat, flatMap}
Reduces the dimension of an array, recursively flattening the array to the specified depth, which defaults to 1.
// Flatten one level: const array = [1, [2, [3]]]; array.flat(); // → [1, 2, [3]] // One of the following outputs is available: // → [1, 2, 3]Copy the code
[2, 3, 4].flatMap((x) => [x, x * 2]);
// → [2, 4, 3, 6, 4, 8]
Copy the code
2019.1.31 update
[2, 3, [2]].flatMap((x) => x + 1); / / [3, 4,"21"]
Copy the code
I just saw that some people have questioned this result, thinking that there is something going on inside the flatMap. This is actually the reason for the addition-operator-plus (+). + converts the array to the base type String via toprimitive. So [2] + 1 is converted to “2” + 1 === “21”. Here’s some evidence:
Tc39. Making. IO/ecma262 / # se… Tc39. Making. IO/ecma262 / # se…
Object.fromEntries
Object.fromentries (object.entries (Object)) ≈ Object
It is similar to the _. FromPairs of Lodash.
String#{trimStart,trimEnd}
Whitespace before and after can be specified to be removed on one side.
const string = ' hello world '; string.trimStart(); / / -'hello world 'string.trimEnd(); / / -' hello world'string.trim(); / / -'hello world'
Copy the code
Symbol.prototype.description
When creating a Symbol using the factory function Symbol (), you can choose to provide a string as a description as an argument:
const sym = Symbol('The description');
Copy the code
Previously, the only way to access the description was to convert the symbol to a string:
assert.equal(String(sym), 'Symbol(The description)');
Copy the code
Now the introduction of the getter Symbol. The prototype. The description with direct access to the description:
assert.equal(sym.description, 'The description');
Copy the code
try {} catch {}
Now try {} catch {} is selectable with an easier way.
In the past
try {} catch (e) {}
Copy the code
now
More proposals:
Github.com/tc39/propos…