您现在的位置是:首页 > 文章详情

ECMAScript 2023 新特性预览

日期:2023-04-19点击:176

ECMAScript 2023 的最终版本预计将于今年 6 月底发布。最新举行的一次 TC39 会议基本已经确定 了 ECMAScript 2023 的新功能列表,预计不会再有任何重大的编辑更改。

本文整理了在 2023 年进入 Stage 4 的几个提案。按照 TC39 的运作流程,每个提案都从 Stage 0 开始,进入 Stage 4 则意味 着该提案已被 ECMAScript 编辑签署同意意见,成为了事实上的标准特性。

Array find from last

提案ArrayTypedArray原型上增加了findLast()findLastIndex() 方法。它们与find()findIndex()做同样的事情,但顺序相反。这两个方法都很方便,可跳过创建临时的重复、突变和混乱的索引减法。

 const isEven = (number) => number % 2 === 0; const numbers = [1, 2, 3, 4];  // from first to the last lookup console.log(numbers.find(isEven)); // 2 console.log(numbers.findIndex(isEven)); // 1  // from last to the first lookup console.log(numbers.findLast(isEven)); // 4 console.log(numbers.findLastIndex(isEven)); // 3

Hashbang Grammar

Hashbang,也称为 shebang,是可执行脚本开头的字符序列,用于定义要运行的程序的解释器。当 Unix 内核的程序加载器执行 JavaScript 程序时,主机会剥离 hashbang 以生成有效源,然后再将其传递给引擎。Hashbang Grammar 提案标准化了它的完成方式。

 

 #!/usr/bin/env node  console.log('hi 👋');

Symbols as WeakMap keys

在 JavaScript 中,Objects 和 Symbols 被保证是唯一并且不能被重新创建的,这使得它们都是WeakMapkeys 的理想候选者。以前的版本或规范只允许以这种方式使用 Objects ,但新的 Symbols as WeakMap keys 提案则提出将 non-registered Symbols 添加到允许的键列表中。

 const weak = new WeakMap(); const key = Symbol("ref"); weak.set(key, "ECMAScript 2023");  console.log(weak.get(key)); // ECMAScript 2023

Change Array by Copy

Array.prototype上的reverse()sort()splice()方法就地改变数组。Change Array by Copy 提案添加了那些返回新 copy 方法的等价物—— toReversed()toSorted()toSpliced()。该提案还添加了一个with()方法,该方法返回一个新的数组,其中给定索引处的元素被替换为给定值,以避免使用 bracket notation 的就地突变。

 const original = [1, 2, 3, 4]; const reversed = original.toReversed();  console.log(original); // [ 1, 2, 3, 4 ]  console.log(reversed); // [ 4, 3, 2, 1 ] 
 const original = [1, 3, 2, 4]; const sorted = original.toSorted();  console.log(original); // [ 1, 3, 2, 4 ]  console.log(sorted); // [ 1, 2, 3, 4 ] 
 const original = [1, 4]; const spliced = original.toSpliced(1, 0, 2, 3);  console.log(original); // [ 1, 4 ]  console.log(spliced); // [ 1, 2, 3, 4 ] 
 const original = [1, 2, 2, 4]; const withThree = original.with(2, 3);  console.log(original); // [ 1, 2, 2, 4 ]  console.log(withThree); // [ 1, 2, 3, 4 ]
原文链接:https://www.oschina.net/news/237616/es2023-preview
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章