当前位置:网站首页>JS和TS中常用特殊字符
JS和TS中常用特殊字符
2022-06-24 12:33:00 【星河梦~】
JS特殊运算符
链判断运算符(安全导航符,可选链)(?.)
var res = {
}
console.log(res && res.data && res.data.name); //undefined
//判断属性的下层对象是否存在,以上写法可以,但是一旦读取的属性太多,写起来繁琐。
console.log(res ?. data ?. name ) ; //undefined
原理:判断左侧对象是否为null或undefined,如果是就返回undefined
res?.data相当于:res == null ? undefined : res.data
<img [src]="pictureDatas[0]?.pictureUrl" useImg [spinner]="true"></img>
如果pictureDatas[0]有值pictureUrl,就会取到里面的pictureUrl。
如果pictureDatas[0]为null或undefined,页面渲染加载的时候也不会报错。
空值判断运算符(??)
console.log(value ? value : ‘空值’) // ’ ’和0也会算成空
console.log(value ?? ‘空值’)
let a = '';
console.log(a ?? '空') // ''
只有运算符左侧的值为null或undefined时,才会返回右侧的值。否则返回左侧的值。
TS特殊运算符
非空断言操作符(!.)
可以用于断言操作对象是非 null 和非 undefined 类型.
console.log(config!.name) //name
如果name在config里面一定存在,那么ts就不会报错。
可选参数(?: )
在ts中,它可以把某个属性声明为可选的。(可选参数与默认参数一定要放在必选参数之后声明。)
Interface Person {
id: number;
name: string;
age?: number; //可选属性
}
空值判断运算符(??)
同JS
强制转化为bool值(!!)
!!是将表达式强制转化为bool值的运算,运算结果为true或false,表达式是什么值,结果就是对应的bool值,不再像!取非。
if(!!a){
//相当于if(a!=null && typeof(a)!=undefined && a!=’’)
//a有内容才执行的代码...
}
!!false=false; //要注意false和“false” 的区别
!!"false"=true;
!!true=true;
!!(NaN || undefined || null || 0 || ' ')=false;
var o={
flag:true}; var test=!!o.flag;//等效于var test=o.flag||false; alert(test);
边栏推荐
- Fbnet/fbnetv2/fbnetv3: Facebook's lightweight network exploration in NAS | lightweight network
- How can a shell script (.Sh file) not automatically close or flash back after execution?
- Opencv learning notes - loading and saving images
- 哪个商业保险养老险好?2022年商业养老保险产品排名
- Automatic reconstruction of pod after modifying resource object
- How to write controller layer code gracefully?
- Google hacking search engine attack and Prevention
- 基于AM335X开发板 ARM Cortex-A8——Acontis EtherCAT主站开发案例
- Identification of new prognostic DNA methylation features in uveal melanoma by 11+ based on methylation group and transcriptome analysis~
- JVM GC garbage collection detailed introduction quick check of learning notes
猜你喜欢

Opencv learning notes - loading and saving images

Insurance app aging service evaluation analysis 2022 issue 06

Linker --- linker

链接器 --- Linker

GLOG from getting started to getting started

"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!

FreeRTOS概述与体验

Opencv learning notes -- Separation of color channels and multi-channel mixing

Install Kali on the U disk and persist it

How to write controller layer code gracefully?
随机推荐
What is the reason why the video intelligent analysis platform easycvr is locally controllable but the superior equipment cannot control the subordinate equipment?
[5 minutes to play lighthouse] create an immersive markdown writing environment
Use go to process millions of requests per minute
The latest hot spot: the use of copper death related genes for tumor prognosis typing!
Continuous testing | test process improvement: practice continuous testing within iterations in coding
怎样购买打新债 开户是安全的吗
New progress in the construction of meituan's Flink based real-time data warehouse platform
A "full cloud" journey of a quasi financial system
单基因泛癌+简单实验就能发表7分+
What should music website SEO do?
OpenGL es shared context for multi-threaded rendering
链接器 --- Linker
National standard platform easygbs administrator assigns roles to sub users and troubleshooting of invalid channels
Install MySQL in docker and modify my CNF profile
"Meng Hua Lu" is about to have a grand finale. It's better to learn it first than to look ahead!
巧妙构思-铁死亡调节因子分型预后发6+
Cryptography series: collision defense and collision attack
9+! Predicting lymph node status from histology of colorectal cancer by deep learning
怎么可以打新债 开户是安全的吗
ArrayList # sublist these four holes, you get caught accidentally