javascript - ConditionalExpression 中的意外赋值
问题描述
我正在尝试更新从 GitHub 获得的库,并且有一个我以前从未见过的奇怪情况,有人可以帮我理解这是什么吗?据我了解_c = _b === null
,这意味着 _c get 的值是 true 或 false ......后来 _c !== undefined 将永远是 true ..我错过了什么吗?
const _b = { isWrappable: true, insertedTag: false };
let _c;
const status = (_c = _b === null || _b === undefined ? undefined : _b.isWrappable) !== null && _c !== undefined ? _c : false;
console.log(status);
status
在这条线之后true
现在,我怎样才能改变这条线以避免Unexpected assignment within ConditionalExpression.
因为我尝试的一切都给了我false
,例如:
const _b = { isWrappable: true, insertedTag: false };
const _c = _b === null;
const test = (_c || _b === undefined ? undefined : _b.isWrappable) !== null && _c !== undefined ? _c : false;
console.log(test);
- _b 是一个变量,它有时可以为 null 或 undefined
谢谢
解决方案
我已经解决了:) 在我的研究中我发现了 Nullish 合并运算符 (??) 并解决了我的问题所以答案是
const _b = {
isWrappable: true,
insertedTag: false
};
const status = _b?.isWrappable ?? false;
console.log(status);
可选的链式操作符和 Nullish 合并操作符解决了这个问题。
推荐阅读
- parse-server - Parse 服务器响应缓慢
- java - Spring Boot 在错误响应实体中添加请求正文
- javafx - 以 30 fps 运行的 3D JavaFX 应用程序
- javascript - 带有循环和 FPS 的事件监听器
- javascript - 使用 Infura 节点的本地 Metamask 帐户
- php - 如何将 Paystack 集成到我的 Laravel 应用程序中?
- java - 在java 8中编写以下代码的可能方法
- css - 无法在 spring 和 thymeleaf 中添加 css 文件
- tsql - 明确排除条件时遇到除以零错误
- java - java - 如何使用作为参数传入的原始方法名称来跟踪java中的递归方法