JavaScript ES2019 8 个新特性4 个阶段

2019-02-2420:47:49WEB前端开发Comments2,723 views字数 2881阅读模式

JavaScript 不断改进和添加更多功能。TC39 已经完成并批准了 ES2019 的这 8 个功能,它有 4 个阶段,这些阶段是:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

  • Stage 0: Strawman
  • Stage 1: Proposals
  • Stage 2: Drafts
  • Stage 3: Candidates
  • Stage 4: Finished/Approved

以下链接可以查看Stage 0Stage 1 – 3Final Stage文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

可选的 Catch 绑定

能够在不使用 catch 绑定的地方选择性地删除它文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

try {
  // trying to use a new ES2019 feature
  // which may not be implemented in other browsers
} catch (unused) {
  // revert back to old way
}
复制代码

现在可以删除未使用的绑定文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

try {
  ...
} catch {
  ...
}
复制代码

JSON 超集

此提议的动机是 JSON 字符串可以包含未转义的 U + 2028 LINE SEPARATOR 和 U + 2029 PARAGRAPH SEPARATOR 字符,而 ECMAScript 字符串则不能。在 ES2019 之前,它会产生错误SyntaxError: Invalid or unexpected token文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

const LS = eval('"\u2028"');
const PS = eval("'\u2029'");
复制代码

符号说明

在 ES2015 中引入符号,具有非常独特的功能。在 ES2019 中,它现在可以提供给定的描述。其目的是避免间接获得所提供的描述Symbol.prototype.toString文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

const mySymbol = Symbol("myDescription");

console.log(mySymbol); // Symbol(myDescription)

console.log(mySymbol.toString()); // Symbol(myDescription)

console.log(mySymbol.description); // myDescription
复制代码

Function.prototype.toString - 修订版

我们之前已经在函数原型中使用了toString方法,但是在 ES2019 中它已被修改并包含函数内的注释,请注意它在Arrow Functions上不起作用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

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.fromEntries

它是 Object.entries 的反向方法,它也是克隆对象的方法之一文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

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
复制代码

注意:任何嵌入式对象/数组都只是通过引用复制。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

格式良好的 JSON.stringify

这也是由同一个人提出的,并且与 JSON 超集特征有关 。ES2019 不是将未配对的代理代码点作为单个 UTF-16 代码单元返回,而是用 JSON 转义序列表示它们文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

// Before
console.log(JSON.stringify("\uD800")); // "�"

// Now ES2019
console.log(JSON.stringify("\uD800")); // "\ud800"
复制代码

String.prototype trimStart 和 trimEnd

我们已经在 String 原型中使用了trim方法,它删除了字符串开头和结尾之间的空格。但是现在开始介绍 ES2019 的 trimStarttrimEnd文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

// 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"
复制代码

Array.prototype flat 和 flatMap

flat方法创建一个新数组,所有子数组元素以递归方式连接到指定的深度。 默认情况下,深度为 1,使数组上第一层嵌套数组变平。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

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 相关,其中它映射数组然后将其展平文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

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 中的一些有用的即将推出的功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

作者:_Kirito
链接:https://juejin.im/post/5c6b6913e51d45209a1ca03c
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/9679.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/9679.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定