跳至主要内容

@babel/plugin-proposal-destructuring-private

将私有解构 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"
]
}

通过 CLI

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

通过 Node API

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

参考