跳至主内容

@babel/plugin-proposal-destructuring-private(私有解构提案插件)

非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

将私有解构语法 var { #y: y } = this 转换为 var y = this.#y

示例

JavaScript
class Foo {
x;
#y;
equalsTo({ x, #y: y }) {
return this.x === x && this.#y === y;
}
}

将被转换为

JavaScript
class Foo {
x;
#y;
equalsTo(_p) {
var { x } = _p, y = _p.#y;
return this.x === x && this.#y === y;
}
}

该插件遵循以下编译器假设:

安装

npm install --save-dev @babel/plugin-proposal-destructuring-private

用法

通过配置文件(推荐)

babel.config.json
{
"plugins": ["@babel/plugin-proposal-destructuring-private"]
}

由于输出代码包含私有字段,若您已使用其他类特性插件(例如 `@babel/plugin-transform-class-properties),请确保将此插件置于其他插件_之前_。

babel.config.json
{
"plugins": [
"@babel/plugin-proposal-destructuring-private",
"@babel/plugin-transform-class-properties"
]
}

通过命令行

Shell
babel --plugins @babel/plugin-proposal-destructuring-private script.js

通过 Node API

JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-destructuring-private"],
});

参考