JavaScript 不断改进和添加更多功能。TC39 已经完成并批准了 ES2019 的这 8 个功能,它有 4 个阶段,这些阶段是:javascript
如下连接能够查看Stage 0,Stage 1 – 3 和Final Stagejava
可以在不使用 catch 绑定的地方选择性地删除它git
try {
// trying to use a new ES2019 feature
// which may not be implemented in other browsers
} catch (unused) {
// revert back to old way
}
复制代码
如今能够删除未使用的绑定github
try {
...
} catch {
...
}
复制代码
此提议的动机是 JSON 字符串能够包含未转义的 U + 2028 LINE SEPARATOR 和 U + 2029 PARAGRAPH SEPARATOR 字符,而 ECMAScript 字符串则不能。在 ES2019 以前,它会产生错误SyntaxError: Invalid or unexpected token
数组
const LS = eval('"\u2028"');
const PS = eval("'\u2029'");
复制代码
在 ES2015 中引入符号,具备很是独特的功能。在 ES2019 中,它如今能够提供给定的描述。其目的是避免间接得到所提供的描述Symbol.prototype.toString
函数
const mySymbol = Symbol("myDescription");
console.log(mySymbol); // Symbol(myDescription)
console.log(mySymbol.toString()); // Symbol(myDescription)
console.log(mySymbol.description); // myDescription
复制代码
咱们以前已经在函数原型中使用了toString
方法,可是在 ES2019 中它已被修改并包含函数内的注释,请注意它在Arrow Functions
上不起做用。post
function /* comment */ foo /* another comment */() {}
// Before
console.log(foo.toString()); // function foo(){}
// Now ES2019
console.log(foo.toString()); // function /* comment */ foo /* another comment */ (){}
// Arrow Syntax
const bar /* comment */ = /* another comment */ () => {};
console.log(bar.toString()); // () => {}
复制代码
它是 Object.entries 的反向方法,它也是克隆对象的方法之一ui
const obj = {
prop1: 1,
prop2: 2
};
const entries = Object.entries(obj);
console.log(entries); // [ [ 'prop1', 1 ], [ 'prop2', 2 ] ]
const fromEntries = Object.fromEntries(entries);
console.log(fromEntries); // Object { prop1: 1, prop2: 2 }
console.log(obj === fromEntries); // false
复制代码
注意:任何嵌入式对象/数组都只是经过引用复制。spa
这也是由同一我的提出的,而且与 JSON 超集特征有关 。ES2019 不是将未配对的代理代码点做为单个 UTF-16 代码单元返回,而是用 JSON 转义序列表示它们prototype
// Before
console.log(JSON.stringify("\uD800")); // "�"
// Now ES2019
console.log(JSON.stringify("\uD800")); // "\ud800"
复制代码
咱们已经在 String 原型中使用了trim
方法,它删除了字符串开头和结尾之间的空格。可是如今开始介绍 ES2019 的 trimStart
和trimEnd
// Trim
const name = " Codedam ";
console.log(name.trim()); // "Codedam"
// Trim Start
const description = " Unlocks Secret Codes ";
console.log(description.trimStart()); // "Unlocks Secret Codes "
// Trim End
const category = " JavaScript ";
console.log(category.trimEnd()); // " JavaScript"
复制代码
flat
方法建立一个新数组,全部子数组元素以递归方式链接到指定的深度。 默认状况下,深度为 1,使数组上第一层嵌套数组变平。
const arr = [1, 2, [3, 4, [5, 6]]];
arr.flat(); // [1, 2, 3, 4, [5, 6]]
arr.flat(2); // [1, 2, 3, 4, 5, 6]
// You can use Infinity to flatten all the nested arrays no matter how deep the array is
const arrExtreme = [1, [2, [3, [4, [5, 6, 7, [8, 9]]]]]];
arrExtreme.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9]
复制代码
flatMap
相似于 flat 而且与 map 相关,其中它映射数组而后将其展平
const arr = ["Codedam", "is Awsome", "!"];
const mapResult = arr.map(item => item.split(" "));
console.log(mapResult); // [ [ 'Codedam' ], [ 'is', 'Awsome' ], [ '!' ] ]
const flatMapResult = arr.flatMap(chunk => chunk.split(" "));
console.log(flatMapResult); // ['Codedam', 'is', 'Awsome', '!'];
复制代码
强调一下如今 Stage 3 中的一些有用的即将推出的功能。
原文:8 NEW FEATURES in JavaScript ES2019
做者:Rienz
翻译:chenyeah.com/posts/a77f9…