For AI agents: the complete documentation index is available at /tc39-atlas/llms.txt, the full documentation bundle is available at /tc39-atlas/llms-full.txt, and this page is available as Markdown at /tc39-atlas/proposals/proposal-destructuring-private.md.
  • 简体中文
  • Destructure Private Fields S2

    中文标题:解构私有字段

    提案概览
    提案速览

    该提案支持解构私有字段,允许开发者使用类似 const { #x: x } = this 的语法将私有类字段提取到局部变量中。它仅限语法层面,避免具体化问题,并根据委员会决定排除简写语法。

    Note

    以下 README 来自上游仓库,其中的阶段或状态标注可能滞后;当前信息以提案概览为准。

    解构私有字段 提案

    一个将私有字段与解构相结合的提案。

    class Foo {
      #x = 1;
    
      constructor() {
        console.log(this.#x); // => 1
        
        const { #x: x } = this;
        console.log(x); // => 1
      }
    
      equals({ #x: otherX }) {
        const { #x: currentX } = this;
        return currentX === otherX;
      }
    }

    提案发起人

    状态

    当前 阶段: 2

    动机

    解构是一种流行的特性,用于存储对象属性的局部引用。私有字段提案旨在支持类实例中私有属性的目标,但只包含了最小必需的功能。不幸的是,这导致了解构被遗漏。

    本提案通过仅限语法的方式来避免私有字段的具体化问题。同时,它也不需要担心“简写”语法(即表达式中 #x 作为 this.#x 的简写),因为委员会已决定在 Ergonomic Brand Checks 提案之后不再追求简写语法。

    相关链接