11.前端动态-第92次TC39会议举行-《前端知识进阶》

admin 2025-11-01 16:01:07 编程 来源:ZONE.CI 全球网 0 阅读模式
  • 1. Array.fromAsync
  • 2. Well-Formed Unicode Strings
  • 3. Extractors

    TC39是一个推动 JavaScript 发展的技术委员会,由各个主流浏览器厂商的代表构成,其主要工作就是制定 ECMAScript 标准。TC39 每两个月举行一次面对面会议,。9 月 13 日至 16 日,第 92 次 TC39 会议举行,以下三个提案取得新进展:

    • Stage 3Array.fromAsync
    • Stage 2Well-Formed Unicode Strings,关于确定字符串是否是格式良好的 Unicode 的方法提议;
    • Stage 1Extractors ,ECMAScript 的提取器。

    对于提案,从提出到最后被纳入ECMAScript 标准,总共分为五步:

    • stage0(strawman):任何TC39的成员都可以提交。
    • stage1(proposal):进入此阶段就意味着这一提案被认为是正式的了,需要对此提案的场景与API进行详尽的描述。
    • stage2(draft):演进到这一阶段的提案如果能最终进入到标准,那么在之后的阶段都不会有太大的变化,因为理论上只接受增量修改。
    • state3(candidate):这一阶段的提案只有在遇到了重大问题才会修改,规范文档需要被全面的完成。
    • state4(finished):这一阶段的提案将会被纳入到ES每年发布的规范之中。

    1. Array.fromAsync

    在 JavaScript 中内置了 Array.from 方法,它用于将类数组或者可迭代对象生成一个新的数组实例。在ECMAScript 2018中引入了异步可迭代对象。而JavaScript中一直缺少直接从异步可迭代对象生成数组的内置方法。proposal-array-from-async 提案中提出来的 Array.fromAsync 方法就是为了解决这个问题而提出来的。

    下面来看一个简单的例子:

    1. async function * asyncGen (n) {
    2. for (let i = 0; i < n; i++)
    3. yield i * 2;
    4. }
    5. // arr 将变为 [0, 2, 4, 6]`
    6. const arr = [];
    7. for await (const v of asyncGen(4)) {
    8. arr.push(v);
    9. }
    10. // 与上述方式是等价的
    11. const arr = await Array.fromAsync(asyncGen(4));

    Array.fromAsync 可以将异步迭代转换为 promise,并将解析为新数组。在 promise 解析之前,它将从输入值中创建一个异步迭代器,进行惰性的迭代,并将每个产生的值添加到新数组中。

    与其他基于 Promise 的 API 一样,Array.fromAsync 总是会立即返回一个 promise。当 Array.fromAsync 的输入在创建其异步或同步迭代器时引发错误时,则此 promise 状态将被置为 rejected

    提案地址:https://github.com/tc39/proposal-array-from-async

    2. Well-Formed Unicode Strings

    ECMAScript 中的字符串都是 UTF-16 编码的,ECMAScript 对整数值没有任何限制或要求,除非它们必须是 16 位无符号整数。在格式良好的字符串中,序列中的每个整数值代表一个 UTF-16 编码的 Unicode 文本的 16 位代码单元。但是,并非所有 UTF-16 代码单元序列都表示 UTF-16 编码的 Unicode 文本。在格式良好的字符串中,0xD800..0xDBFF 和 0xDC00..0xDFFF范围内的代码单元必须成对出现,并按顺序排列。具有不成对或无序代理项的字符串是格式不正确。

    在 JavaScript 中,这可以通过正则表达式测试来实现:

    1. !/\p{Surrogate}/u.test(str);

    或者使用以下算法:

    1. function isWellFormed(str) {
    2. for (let i = 0; i < str.length; ++i) {
    3. const isSurrogate = (str.charCodeAt(i) & 0xF800) == 0xD800;
    4. if (!isSurrogate) {
    5. continue;
    6. }
    7. const isLeadingSurrogate = str.charCodeAt(i) < 0xDC00;
    8. if (!isLeadingSurrogate) {
    9. return false; // unpaired trailing surrogate
    10. }
    11. const isFollowedByTrailingSurrogate = i + 1 < str.length && (str.charCodeAt(i + 1) & 0xFC00) == 0xDC00;
    12. if (!isFollowedByTrailingSurrogate) {
    13. return false; // unpaired leading surrogate
    14. }
    15. ++i;
    16. }
    17. return true;
    18. }

    目前没有内置的API 可以实现这一点。因此,Well-Formed Unicode Strings 提案就是在 ECMA-262 中定义一种方法来验证给定的 ECMAScript 字符串是否格式正确。它提出了两种方法:

    • String.prototype.isWellFormed():用于判断字符串格式是否正确;
    • String.prototype.toWellFormed():用于将字符串转换为正确格式。

      提案地址:https://github.com/tc39/proposal-is-usv-string

    3. Extractors

    ECMAScript 目前没有在解构期间执行用户定义逻辑的机制,这意味着与数据验证和转换相关的操作可能需要多个语句:

    1. function toInstant(value) {
    2. if (value instanceof Temporal.Instant) {
    3. return value;
    4. } else if (value instanceof Date) {
    5. return Temporal.Instant.fromEpochMilliseconds(+value);
    6. } else if (typeof value === "string") {
    7. return Temporal.Instant.from(value);
    8. } else {
    9. throw new TypeError();
    10. }
    11. }
    12. class Book {
    13. constructor({
    14. isbn,
    15. title,
    16. createdAt = Temporal.Now.instant(),
    17. modifiedAt = createdAt
    18. }) {
    19. this.isbn = isbn;
    20. this.title = title;
    21. this.createdAt = toInstant(createdAt);
    22. // 如果 `modifiedAt` 是 `undefined`,就会重复一些工作
    23. this.modifiedAt = toInstant(modifiedAt);
    24. }
    25. }
    26. new Book({ isbn: "...", title: "...", createdAt: Temporal.Instant.from("...") });
    27. new Book({ isbn: "...", title: "...", createdAt: new Date() });
    28. new Book({ isbn: "...", title: "...", createdAt: "..." });

    Extractors for ECMAScript 提案就是在 ECMAScript 中引入提取器(又名“提取器对象”)。提取器将增加 BindingPatternAssignmentPattern 的语法以允许新的解构形式,如下例所示:

    1. // binding patterns
    2. const Foo(y) = x; // instance-array destructuring
    3. const Foo{y} = x; // instance-object destructuring
    4. const [Foo(y)] = x; // nesting
    5. const [Foo{y}] = x; // ..
    6. const { z: Foo(y) } = x; // ..
    7. const { z: Foo{y} } = x; // ..
    8. const Foo(Bar(y)) = x; // ..
    9. const X.Foo(y) = x; // qualified names (i.e., a.b.c)
    10. // assignment patterns
    11. Foo(y) = x; // instance-array destructuring
    12. Foo{y} = x; // instance-object destructuring
    13. [Foo(y)] = x; // nesting
    14. [Foo{y}] = x; // ..
    15. ({ z: Foo(y) } = x); // ..
    16. ({ z: Foo{y} } = x); // ..
    17. Foo(Bar(y)) = x; // ..
    18. X.Foo(y) = x; // qualified names (i.e., a.b.c)

    此外,这将利用 Pattern Matching 提案新添加的 Symbol.matcher。 使用新形式进行解构时,将调用 Symbol.matcher 方法,并将其结果进行解构。

    使用 Extractors,可以在绑定模式中封装和重用验证和转换逻辑:

    1. const InstantExtractor = {
    2. [Symbol.matcher]: value =>
    3. value instanceof Temporal.Instant ? { matched: true, value: [value] } :
    4. value instanceof Date ? { matched: true, value: [Temporal.Instant.fromEpochMilliseconds(value.getTime())] } :
    5. typeof value === "string" ? { matched: true, value: [Temporal.Instant.from(value)] } :
    6. { matched: false };
    7. }
    8. };
    9. class Book {
    10. constructor({
    11. isbn,
    12. title,
    13. createdAt: InstantExtractor(createdAt) = Temporal.Now.instant(),
    14. modifiedAt: InstantExtractor(modifiedAt) = createdAt
    15. }) {
    16. this.isbn = isbn;
    17. this.title = title;
    18. this.createdAt = createdAt;
    19. this.modifiedAt = modifiedAt;
    20. }
    21. }
    22. new Book({ isbn: "...", title: "...", createdAt: Temporal.Instant.from("...") });
    23. new Book({ isbn: "...", title: "...", createdAt: new Date() });
    24. new Book({ isbn: "...", title: "...", createdAt: "..." });

    提案地址:https://github.com/tc39/proposal-extractors

    相关链接:

    • isWellFormed()toWellFormed():https://tc39.es/proposal-is-usv-string/
    • Pattern Matching 提案:https://github.com/tc39/proposal-pattern-matching
    以太坊cppgolang区别 编程

    以太坊cppgolang区别

    以太坊是一种去中心化的开源平台,它采用智能合约技术,旨在构建和运行不受干扰的分布式应用程序。作为目前最受欢迎的区块链平台之一,以太坊提供了多种编程语言的支持,其
    progolang 编程

    progolang

    Go语言(Golang)是由Google开发的一门静态类型编程语言。作为一名专业的Golang开发者,我深知这门语言的优势和特点。在本文中,我将介绍Golang
    golangn个发送者 编程

    golangn个发送者

    Golang是一种开源的编程语言,由Google团队开发,旨在提高程序的并发性和简化软件开发过程。在Go语言中,有时需要向多个接收者发送信息。本文将介绍如何在G
    golang技能图谱 编程

    golang技能图谱

    从互联网行业的快速发展到人工智能技术的日益成熟,各种编程语言也应运而生。而在这众多的编程语言中,Golang(即Go)作为一门强大且高效的开发语言备受关注。Go
    评论:0   参与:  12