Skip to main content

@babel/plugin-transform-private-methods

信息

此插件包含在 @babel/preset-env 中,在 ES2022

¥This plugin is included in @babel/preset-env, in ES2022

History
版本变化
v7.3.0支持私有访问器(getters 和 setters)
v7.2.0初始发行

示例

¥Example

JavaScript
class Counter extends HTMLElement {
#xValue = 0;

get #x() {
return this.#xValue;
}
set #x(value) {
this.#xValue = value;
window.requestAnimationFrame(this.#render.bind(this));
}

#clicked() {
this.#x++;
}
}

安装

¥Installation

npm install @babel/plugin-transform-private-methods --save-dev

用法

¥Usage

¥With a configuration file (Recommended)

没有选项:

¥Without options:

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

有选项:

¥With options:

babel.config.json
{
"plugins": [["@babel/plugin-transform-private-methods", { "loose": true }]]
}

通过 CLI

¥Via CLI

Shell
$ babel --plugins @babel/plugin-transform-private-methods script.js

通过 Node API

¥Via Node API

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

选项

¥Options

loose

boolean,默认为 false

¥boolean, defaults to false.

注意

loose 模式配置设置必须与 @babel/plugin-transform-class-properties 相同。

¥The loose mode configuration setting must be the same as @babel/plugin-transform-class-properties.

如果为 true,私有方法将通过 Object.defineProperty 而不是 WeakSet 直接分配给其父级。这会提高性能和调试(与 .get() 相比,正常的属性访问),但代价是可能通过 Object.getOwnPropertyNames 之类的东西泄漏 "privates"。

¥When true, private methods will be assigned directly on its parent via Object.defineProperty rather than a WeakSet. This results in improved performance and debugging (normal property access vs .get()) at the expense of potentially leaking "privates" via things like Object.getOwnPropertyNames.

提醒

考虑迁移到顶层 privateFieldsAsProperties 假设。

¥Consider migrating to the top level privateFieldsAsProperties assumption.

babel.config.json
{
"assumptions": {
"privateFieldsAsProperties": true,
"setPublicClassFields": true
}
}

请注意,privateFieldsAsPropertiessetPublicClassFields 都必须设置为 true

¥Note that both privateFieldsAsProperties and setPublicClassFields must be set to true.

让我们使用以下示例:

¥Let's use the following as an example:

JavaScript
class Foo {
constructor() {
this.publicField = this.#privateMethod();
}

#privateMethod() {
return 42;
}
}

默认情况下,这变为:

¥By default, this becomes:

JavaScript
var Foo = function Foo() {
"use strict";

babelHelpers.classCallCheck(this, Foo);

_privateMethod.add(this);

this.publicField = babelHelpers
.classPrivateMethodGet(this, _privateMethod, _privateMethod2)
.call(this);
};

var _privateMethod = new WeakSet();

var _privateMethod2 = function _privateMethod2() {
return 42;
};

使用 { privateFieldsAsProperties: true },它变为:

¥With { privateFieldsAsProperties: true }, it becomes:

JavaScript
var Foo = function Foo() {
"use strict";

babelHelpers.classCallCheck(this, Foo);
Object.defineProperty(this, _privateMethod, {
value: _privateMethod2,
});
this.publicField = babelHelpers
.classPrivateFieldLooseBase(this, _privateMethod)
[_privateMethod]();
};

var _privateMethod = babelHelpers.classPrivateFieldLooseKey("privateMethod");

var _privateMethod2 = function _privateMethod2() {
return 42;
};
提示

你可以阅读有关配置插件选项 此处 的更多信息

¥You can read more about configuring plugin options here

参考

¥References