@babel/plugin-transform-nullish-coalescing-operator
資訊
此外掛程式包含在 @babel/preset-env
中,位於 ES2020
範例
在
JavaScript
var foo = object.foo ?? "default";
輸出
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」。
安裝
- npm
- Yarn
- pnpm
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
用法
使用組態檔(建議)
babel.config.json
{
"plugins": ["@babel/plugin-transform-nullish-coalescing-operator"]
}
透過 CLI
Shell
babel --plugins @babel/plugin-transform-nullish-coalescing-operator script.js
透過 Node API
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-transform-nullish-coalescing-operator"],
});
選項
loose
boolean
,預設為 false
。
當為 true
時,此轉換會假裝 document.all
不存在,並使用與 null
的寬鬆相等性檢查,而不是對 null
和 undefined
進行嚴格相等性檢查。
注意
考慮轉移至頂層 noDocumentAll
假設。
babel.config.json
{
"assumptions": {
"noDocumentAll": true
}
}
範例
在
JavaScript
var foo = object.foo ?? "default";
輸出
JavaScript
var _object$foo;
var foo = (_object$foo = object.foo) != null ? _object$foo : "default";
提示
您可以在 這裡 閱讀更多關於配置外掛選項的資訊