当前位置:网站首页>JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax
JS logic empty allocation double question mark syntax, double vertical bar syntax and optional chain syntax
2022-06-10 00:25:00 【Eternity l】
The syntax of the optional chain allows developers to access deeper nested object properties , Don't worry about whether the attribute really exists . in other words , If the optional chain encounters null or undefined Value , Through a short circuit (short-circuit) Calculation , return undefined, And never report a mistake .
Logical null assignment operator is only used when null value or undefined (null or undefined) The value is assigned to a
Double question mark : When the previous value is null 、undefined when , Return the following value , Otherwise, take the previous value
user.userinfo.age ?? 0Double vertical bar : As long as the previous value is converted to a Boolean value of false when , Take the following value ( Such as :undefined、null、false、 An empty string 、 The number 0)
reflection : Double question marks are more appropriate when variables are unknown Whether to define perhaps Assignment or not The scenario used
?. Optional chain : If it means
let test = {
value: {
a: '1222'
}
}
If we want to achieve test In existence , return test.value, Otherwise return to undefined
It needs to be written like this :
let myvalue = test ? test. value : undefined
If you use the optional chain Syntax , It can be abbreviated as :
let myvalue = test ?. value
If you want to go back test.value.a , It can be abbreviated as :
let a = test ?.value?.a || 10 // If test.value.a If it doesn't exist ( by null or undefined) , Then return to 10 Fixed value


Reference resources :
Know optional chain and double question mark Syntax _ Front end discovery blog -CSDN Blog
边栏推荐
- Is Huishang futures regular? Is it safe to open an account?
- Illustration Google V8 06: prototype chain: how does V8 implement object inheritance?
- 嵌入式商业智能软件Wyn Enterprise正式步入V5.0时代!
- 设计千万级学生管理系统的考试试卷存储方案
- (转)ArcGIS中ObjectID,FID和OID字段有什么区别?
- Py6s configuration tutorial (win10 × 64)
- Baidu post-95 programmers were sentenced for deleting databases and running away. The motivation was the change of work content and dissatisfaction with leaders. What impact would deleting databases b
- Virtual machine environment configuration record 1
- Johns Hopkins, Berkeley, California, etc. | using machine learning of sequence and structural features to achieve Generalized Prediction of antibody thermal stability
- 电脑连接打印机出现的问题(打印机显示未指定)解决方法
猜你喜欢

Niuke.com: median in data flow

可重复读隔离级别的基石MVCC机制

没有项目管理经验,可以参加PMP考试?

Do your filial duty to make an old people's fall prevention alarm system for your family

MySQL执行计划

The problem of connecting the computer to the printer (the printer display is not specified) solution

网络状态的起起伏伏

Opencv architecture

Idea uninstall tutorial

The essence and soul of message queue
随机推荐
« Lorsque vous n'êtes plus programmeur, beaucoup de choses échappent au contrôle » - conversation avec Suse CTO, la plus grande entreprise open source indépendante au monde
The number of encrypted assets covered by Bloomberg terminal products expanded to the top 50 assets
Record a malicious keyword brushed by illegal drainage
BS based_ Pagination query function of pagination plug-in
I've taken it. The MySQL table has 500W rows. Is there anyone who doesn't partition?
MATLAB中对tif格式栅格影像读取-保存
有没有必要考取PMP?
Reading and saving TIF format raster image in MATLAB
C # WPF realizes dynamic increase and decrease of tab page
彭博终端产品覆盖的加密资产数量扩大为前50名资产
数字大时代来临,360携手创业黑马助力中小企业抓住关键未来
Dynamic reading of protobuf data
732. 我的日程安排表 III
2022-06-10日报: 华为十大发明公布:高效加法网络、多目标博弈智能驾驶获奖
Blue Bridge Cup_ Split cube_ Combinatorial mathematics_ Addition principle
Baidu post-95 programmers were sentenced for deleting databases and running away. The motivation was the change of work content and dissatisfaction with leaders. What impact would deleting databases b
"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
Is it necessary to get PMP?
Who says redis can't save big keys
June 10, 2022 Daily: Huawei's top ten inventions were announced: efficient addition network and multi-objective game intelligent driving won the prize