跳到主要内容

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

信息

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";
备注

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​

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

Via CLI​

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

Via Node API​

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

Options​

loose​

boolean, defaults to false.

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.

警告

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​