Skip to main content

@babel/plugin-transform-logical-assignment-operators

信息

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

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

示例

¥Example

输入

¥In

JavaScript
a ||= b;
obj.a.b ||= c;

a &&= b;
obj.a.b &&= c;

输出

¥Out

JavaScript
var _obj$a, _obj$a2;

a || (a = b);
(_obj$a = obj.a).b || (_obj$a.b = c);

a && (a = b);
(_obj$a2 = obj.a).b && (_obj$a2.b = c);

使用 Nullish 合并

¥With Nullish Coalescing

使用 @babel/plugin-transform-nullish-coalescing-operator 插件时(包含在 @babel/preset-env 中)

¥While using the @babel/plugin-transform-nullish-coalescing-operator plugin (included in @babel/preset-env)

JavaScript
a ??= b;
obj.a.b ??= c;
JavaScript
var _a, _obj$a, _obj$a$b;

(_a = a) !== null && _a !== void 0 ? _a : (a = b);
(_obj$a$b = (_obj$a = obj.a).b) !== null && _obj$a$b !== void 0
? _obj$a$b
: (_obj$a.b = c);

安装

¥Installation

npm install --save-dev @babel/plugin-transform-logical-assignment-operators

用法

¥Usage

¥With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-logical-assignment-operators"]
}

通过 CLI

¥Via CLI

Shell
babel --plugins @babel/plugin-transform-logical-assignment-operators script.js

通过 Node API

¥Via Node API

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

参考

¥References