当前位置:网站首页>js 逻辑空分配双问号语法 、双竖杠语法 与 可选链语法
js 逻辑空分配双问号语法 、双竖杠语法 与 可选链语法
2022-06-09 23:46:00 【Eternity l】
可选链的语法允许开发者访问嵌套得更深的对象属性,而不用担心属性是否真的存在。也就是说,如果可选链在挖掘过程遇到了null或undefined的值,就会通过短路(short-circuit)计算,返回undefined,而不会报错。
逻辑空分配运算符仅在空值或未定义(null or undefined)时才将值分配给 a
双问号:当前面的值为 null 、undefined 时,就返回后面的值,否则取前面的值
user.userinfo.age ?? 0双竖杠:只要前面的值转为布尔值为false时,就取后面的值(如:undefined、null、false、空字符串、数值0)
思考:双问号更适合在不知道变量 是否定义 或者 是否赋值 时的场景使用
?.可选链: 如果要表示
let test = {
value: {
a: '1222'
}
}
如果要实现 test 存在时,返回 test.value,否则返回 undefined
则需要这样写:
let myvalue = test ? test. value : undefined
如果用可选链语法,则可以简写为:
let myvalue = test ?. value
如果要返回 test.value.a ,则可以简写为:
let a = test ?.value?.a || 10 //如果test.value.a不存在的话 (为null或undefined) ,则返回10固定值


参考:
vue项目支持js新语法可选链“?.“以及逻辑空分配(双问号)“??“_鸽子.Luna的博客-CSDN博客_vue 双问号
边栏推荐
- C#实用技巧之:将图标打包进DLL并读取
- 嵌入式商业智能软件Wyn Enterprise正式步入V5.0时代!
- “当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO
- 46岁新晋院士:我上研究生时,卸载了所有游戏
- 彭博终端产品覆盖的加密资产数量扩大为前50名资产
- ArcMap解决几何错误
- 查看本机电脑wheel可安装版本号
- Idea uninstall tutorial
- Detailed explanation of IEEE 754 floating point number standard
- Spingboot+quartrz cluster version realizes dynamic timing tasks (using reflection to realize custom services)
猜你喜欢

Q-learning

"When you are no longer a programmer, many things will get out of control" -- dialogue with SUSE CTO, the world's largest independent open source company

消息队列的精髓与灵魂

Illustration Google V8 06: prototype chain: how does V8 implement object inheritance?

Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability

Opencv architecture

10 common high-frequency business scenarios that trigger IO bottlenecks

10个常见触发IO瓶颈的高频业务场景

低边驱动和高边驱动

SIGIR 2022 | HKU and Wuhan University put forward kgcl: a recommendation system based on knowledge map comparative learning
随机推荐
嵌入式商业智能软件Wyn Enterprise正式步入V5.0时代!
Do you know about singleton mode? I don't understand anyway.
DDD驱动领域设计学习笔记
MySql事务
请问徽商期货是正规的吗?开户交易安全吗?
Do your filial duty to make an old people's fall prevention alarm system for your family
MySQL transaction
bias、variance介绍
JVM explanation
If (obj! = null) is not required
Is Huishang futures regular? Is it safe to open an account?
Idea uninstall tutorial
46岁新晋院士:我上研究生时,卸载了所有游戏
View the installable version number of the wheel on this computer
IOS cache - nscache and sandbox cache
Who says redis can't save big keys
“当你不再是程序员,很多事会脱离掌控”—— 对话全球最大独立开源公司SUSE CTO
C # WPF background dynamic add control (Classic)
Introduction to bias and variance
Core [email protected] < 3.4 is no longer maintained and not recommended for usage due to the