Skip to main content

@babel/plugin-proposal-destructuring-private

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

¥Transforms private destructuring var { #y: y } = this to var y = this.#y.

示例

¥Example

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

将转变为

¥will be transformed to

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

该插件尊重这些编译器假设:

¥The plugin respects these compiler assumptions:

安装

¥Installation

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

用法

¥Usage

¥With a configuration file (Recommended)

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

由于输出代码包含私有字段,如果你已经在使用其他类功能插件(例如 `@babel/plugin-transform-class-properties),请务必将其放在其他插件之前。

¥Because the output code includes private fields, if you are already using other class feature plugins (e.g. `@babel/plugin-transform-class-properties), be sure to place it before the others.

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

通过 CLI

¥Via CLI

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

通过 Node API

¥Via Node API

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

参考

¥References