当前位置:网站首页>JS 问号?妙用
JS 问号?妙用
2022-08-04 18:36:00 【蓓蕾心晴】
// 1. ?
var obj ={a:1,b:2}
obj.c.a
// 会报错:Uncaught TypeError: Cannot read properties of undefined,因为obj.c 不存在
解决方法:
obj.c?.a
// 等价于
obj.c && obj.c.a
// ?是可选链操作符,在引用为空null 或者 undefined 的情况下不会引起错误,该表达式短路返回值是 undefined
// 2. ??
// 空值合并运算符,在ES2020中新增,可以用来保证值不为 null 或者 undefined
var a1="";
var b1=a1??"default1" // ""
var a2=undefined;
var b2=a2??"default2" // default2
var a3=null;
var b3=a3??"default3" // default3
// 注:(??) 直接和 AND(&&)和 OR(||)一起用会报错,如果要一起用,记得单独给 ?? 左右表达式加括号,如:
b=(a??"dffdfdf") || 123 // dffdfdf
b=(a??"dffdfdf") && 123 // 123
// 3. 逻辑或(||)
// 空值合并运算符将跳过 null,undefined
// 逻辑或运算符会跳过 null,undefined,false
false ?? 'hello' // false
false || 'hello' // 'hola'
// 如果不想用要虚值,可以使用 ||。如果只想检查是否为 null 或 undefined,就用 ??边栏推荐
- 基于激励的需求响应计划下弹性微电网的短期可靠性和经济性评估(Matlab代码实现)
- 如何进行自动化测试?
- 情绪的波动起伏
- Enterprise survey correlation analysis case
- 【web自动化测试】Playwright快速入门,5分钟上手
- PHP代码审计10—命令执行漏洞
- 巴比特 | 元宇宙每日必读:微博动漫将招募全球各类虚拟偶像并为其提供扶持...
- Babbitt | Metaverse daily must-read: Weibo animation will recruit all kinds of virtual idols around the world and provide support for them...
- Kubernetes入门到精通- Operator 模式入门
- 基于 eBPF 的 Kubernetes 可观测实践
猜你喜欢

如何进行自动化测试?

limux入门3—磁盘与分区管理

部署LVS-DR群集

EuROC 数据集格式及相关代码

DHCP&OSPF combined experimental demonstration (Huawei routing and switching equipment configuration)

EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?

Alibaba Cloud International Edition uses ROS to build WordPress tutorial

ptables基本语法使用规则

Win10只读文件夹怎么删除

How does EasyCVR call the double-speed playback of device recording through the interface?
随机推荐
当前最快的实例分割模型:YOLACT 和 YOLACT++
基于 eBPF 的 Kubernetes 可观测实践
阿里云国际版使用ROS搭建WordPress教程
How does the intelligent video surveillance platform EasyCVR use the interface to export iframe addresses in batches?
斯坦福:未来的RGB LED可以贴在你的皮肤上
EasyCVR calls the cloud recording API and returns an error and no recording file is generated. What is the reason?
如何进行自动化测试?
【STM32】STM32单片机总目录
PHP代码审计9—代码执行漏洞
关于使用腾讯云HiFlow场景连接器每天提醒签到打卡
部署LVS-DR群集
22/8/4 记忆化搜索+博弈论
开发那些事儿:如何通过EasyCVR平台获取监控现场的人流量统计数据?
智能视频监控平台EasyCVR如何使用接口批量导出iframe地址?
#yyds干货盘点# 面试必刷TOP101:链表相加(二)
合宙Cat1 4G模块Air724UG配置RNDIS网卡或PPP拨号,通过RNDIS网卡使开发板上网(以RV1126/1109开发板为例)
EuROC 数据集格式及相关代码
FE01_OneHot-Scala Application
EasyCVR本地接入国标设备映射公网后,本地设备出现无法播放与级联的解决方法
dotnet core 输出调试信息到 DebugView 软件