跳到主要內容

@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"],
});

參考