@babel/plugin-transform-nullish-coalescing-operator
示例
¥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 == null 和 document.all 已被视为不是 "nullish"。
¥We cannot use != null here because document.all == null and
document.all has been deemed not "nullish".
安装
¥Installation
- npm
- Yarn
- pnpm
- Bun
npm install --save-dev @babel/plugin-transform-nullish-coalescing-operator
yarn add --dev @babel/plugin-transform-nullish-coalescing-operator
pnpm add --save-dev @babel/plugin-transform-nullish-coalescing-operator
bun add --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 执行松散的相等检查,而不是对 null 和 undefined 进行严格的相等检查。
¥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";
参考
¥References