W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
NOTE: This plugin is included in @babel/preset-env, in ES2022
A class with a static block will be transformed into a static private property, whose initializer is the static block wrapped in an IIAFE (immediate invoked arrow function expression).
JavaScript
class C {
static #x = 42;
static y;
static {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
}
}
will be transformed to
JavaScript
class C {
static #x = 42;
static y;
static #_ = (() => {
try {
this.y = doSomethingWith(this.#x);
} catch {
this.y = "unknown";
}
})();
}
Because the output code includes private class properties, if you are already using other class feature plugins (e.g. `@babel/plugin-proposal-class-properties), be sure to place it before the others.
babel.config.json
{
"plugins": [
"@babel/plugin-proposal-class-static-block",
"@babel/plugin-proposal-class-properties"
]
}
pnpm add --save-dev @babel/plugin-proposal-class-static-block
babel.config.json
{
"plugins": ["@babel/plugin-proposal-class-static-block"]
}
Shell
babel --plugins @babel/plugin-proposal-class-static-block script.js
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-class-static-block"],
});
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: