Cross post from h3manth.com.
So, it is time for an other post, this time it's ES2019 features.
Without wasting time with some placeholder content, the below are the features and examples:
Array#{flat,flatMap}
[1, 2, 3].flatMap((x) => [x, x * 2]);
// => [1, 2, 2, 4, 3, 6]
[1, [2, [3]]].flat(Infinity);
// => [1, 2, 3]
Object.fromEntries
const iterableOfEntries = new Map([
['cat', 'dog'],
['life', 42]
]);
const obj = Object.fromEntries(iterableOfEntries);
console.log(obj); // { cat: "dog", life: 42 }
String#{trimStart,trimEnd}
" Hey JS!".trimStart(); // "Hey JS!"
"Hey JS! ".trimEnd(); // "Hey JS!"
Symbol#description
const symbol = Symbol('TC39');
console.log(symbol.description); // 'TC39'
console.log(symbol.hasOwnProperty('description')); // false
try { } catch {} // optional binding
try {
throw new Error("End of life!");
} catch { // ✋
console.log("^ no params for catch, you are dead anyway!");
}
JSON ⊂ ECMAScript
// extend ECMA-262 syntax into a superset of JSON.
const LS = "";
const PS = eval("'\u2029'");
well-formed JSON.stringify
JSON.stringify('\uD800');
// → '"\\ud800"'
Function#toString
function /* this is bar */ bar () {}
bar.toString(); // 'function /* this is bar */ bar () {}'
// ^ perviously this was not the case.
Array#sort stability
[
{ name: "Jan", age: 20 },
{ name: "Jhon", age: 20 },
{ name: "David", age: 18 },
{ name: "Ram", age: 18 },
{ name: "Sita", age: 18 },
{ name: "Ravan", age: 18 },
{ name: "Asura", age: 12 },
{ name: "Milly", age: 12 },
].sort((m, n) => m.age - n.age));
// People with the same age retain their order.
Don't miss:
🎉 New JavaScript features in ES2019:
— Mathias Bynens (@mathias ) January 29, 2019
➡️ 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