对下一版本的JavaScript官方规范的建议,介绍了对字符串、承诺和对象引用的增强。
信息世界》杂志的总编辑 |

Thinkstock
ECMAScript 2021是计划中的JavaScript官方规范的下一版本,它将增加字符串和承诺的功能。
该规范由ECMA国际组织管辖。ECMAScript 2021预计将于6月正式批准,将包括以下功能。
String.prototype.ReplaceAll,为开发者提供了一种替换字符串中子串的所有实例的方法,而不需要使用全局的regexp。There is evidence that developers are trying to accomplish this in JavaScript, with a global regexp currently the most-common way of achieving this. `String.prototype.replace`, meanwhile, affects only the first occurrence when used with a string argument. The proposed solution would add a `replaceAll` method to the String prototype, providing developers with a straightforward solution.* `AggregateError`, a new error type to represent multiple errors at once. * `[Promise.any](https://github.com/tc39/proposal-promise-any)`, a promise combinator that short-circuits when an input value is fulfilled. `Promise.any` accepts an iterable of promises and returns a single promise that resolves with the value of that promise. If not promises of the iterable fulfill, then the return promise is rejected with `AggregateError`. This method is the opposite of `Promise.all`. * [`WeakRef`](https://github.com/tc39/proposal-weakrefs), for referring to a target object without preserving it from garbage collection. This proposal encompasses major new pieces of functionality: creating weak references to objects with the `WeakRefcode` class, and running user-defined finalizers after objects are garbage collected, using the `FinalizationRegistry` class. `WeakRef` and `FinalizationRegistry`, another new feature, are considered advanced features, with their correct use requiring careful thought. They are best avoided, if possible. * `FinalizationRegistry`, to manage registration and de-registration of cleanup operations performed when target objects are garbage collected. * `Array.prototype.sort` is being made more precise, to reduce the amount of cases resulting in an [implementation-defined](https://tc39.es/ecma262/#implementation-defined) sort order. * [Separators for numeric literals](https://github.com/tc39/proposal-numeric-separator), enabling developers to make numeric literals more readable by creating a visual separation between groups of digits. Large numeric literals are difficult to parse visually, particularly when there are long digit repetitions. * [Logical assignment operators](https://github.com/tc39/proposal-logical-assignment), combining logical operators and assignment expressions. Underlying this proposal is the reasoning that there are a dozen mathematical assignment operators but none for often-used logical operators.
ECMAScript 2021 follows [ECMAScript 2020](https://www.infoworld.com/article/3538809/ecmascript-2020-spec-for-javascript-approved.html), which was formally approved last June. ECMAScript 2020 introduced features ranging from a new i`mport` facility for loading modules to a `BigInt` type to work with arbitrary precision integers.
Related: * [JavaScript](https://www.infoworld.com/category/javascript) * [Programming Languages](https://www.infoworld.com/category/programming-languages) * [Software Development](https://www.infoworld.com/category/application-development) Paul Krill is an editor at large at InfoWorld, whose coverage focuses on application development. Follow * [](https://www.infoworld.com/author/Paul-Krill/) * [](mailto:paul_krill@idg.com) * [](https://twitter.com/pjkrill) * [](https://www.infoworld.com/author/Paul-Krill/index.rss) Copyright © 2021 IDG Communications, Inc. * [Stay up to date with InfoWorld’s newsletters for software developers, analysts, database programmers, and data scientists](https://www.infoworld.com/newsletters/signup.html). * [Get expert insights from our member-only Insider articles](https://www.infoworld.com/insider/).