Skip to main content

@babel/plugin-transform-nullish-coalescing-operator

信息

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

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

示例

¥Example

输入

¥In

JavaScript
var foo = object.foo ?? "default";

输出

¥Out

JavaScript
var _object$foo;

var foo =
(_object$foo = object.foo) !== null && _object$foo !== void 0
? _object$foo
: "default";
注意

我们不能在这里使用 != null,因为 document.all == nulldocument.all 已被视为不是 "nullish"。

¥We cannot use != null here because document.all == null and document.all has been deemed not "nullish".

安装

¥Installation

npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator

用法

¥Usage

¥With a configuration file (Recommended)

babel.config.json
{
"plugins": ["@babel/plugin-transform-nullish-coalescing-operator"]
}

通过 CLI

¥Via CLI

Shell
babel --plugins @babel/plugin-transform-nullish-coalescing-operator script.js

通过 Node API

¥Via Node API

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

选项

¥Options

loose

boolean,默认为 false

¥boolean, defaults to false.

true 时,此转换将假装 document.all 不存在,并对 null 执行松散的相等检查,而不是对 nullundefined 进行严格的相等检查。

¥When true, this transform will pretend document.all does not exist, and perform loose equality checks with null instead of strict equality checks against both null and undefined.

提醒

考虑迁移到顶层 noDocumentAll 假设。

¥Consider migrating to the top level noDocumentAll assumption.

babel.config.json
{
"assumptions": {
"noDocumentAll": true
}
}

示例

¥Example

输入

¥In

JavaScript
var foo = object.foo ?? "default";

输出

¥Out

JavaScript
var _object$foo;

var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
提示

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

¥You can read more about configuring plugin options here

参考

¥References