当前位置:网站首页>Common special characters in JS and TS
Common special characters in JS and TS
2022-06-24 12:40:00 【Star River dream~】
Catalog
JS Special operators
Chain judgment operator ( Safety navigator , Optional chain )(?.)
var res = {
}
console.log(res && res.data && res.data.name); //undefined
// Determine whether the underlying object of the attribute exists , The above wording is OK , But once too many properties are read , It's cumbersome to write .
console.log(res ?. data ?. name ) ; //undefined
principle : Judge whether the object on the left is null or undefined, If so, go back undefined
res?.data amount to :res == null ? undefined : res.data
<img [src]="pictureDatas[0]?.pictureUrl" useImg [spinner]="true"></img>
If pictureDatas[0] Valuable pictureUrl, It will be taken inside pictureUrl.
If pictureDatas[0] by null or undefined, There will be no error when the page rendering is loaded .
Null judgment operator (??)
console.log(value ? value : ‘ Null value ’) // ’ ’ and 0 It will be empty
console.log(value ?? ‘ Null value ’)
let a = '';
console.log(a ?? ' empty ') // ''
Only the value to the left of the operator is null or undefined when , To return the value on the right . Otherwise, return the value on the left .
TS Special operators
Non empty assertion operators (!.)
It can be used to assert whether the operation object is right or not null He Fei undefined type .
console.log(config!.name) //name
If name stay config There must be something in it , that ts You can't report an error .
Optional parameters (?: )
stay ts in , It can put a Property is declared as optional Of .( Optional parameters and default parameters must be declared after the required parameters .)
Interface Person {
id: number;
name: string;
age?: number; // Optional attribute
}
Null judgment operator (??)
Same as JS
Forced conversion to bool value (!!)
!! yes Force the expression to bool value Arithmetic , The result of operation is true or false, What is the value of an expression , The result is the corresponding bool value , No longer like ! Take the .
if(!!a){
// amount to if(a!=null && typeof(a)!=undefined && a!=’’)
//a Code that is executed only when there is content ...
}
!!false=false; // it is to be noted that false and “false” The difference between
!!"false"=true;
!!true=true;
!!(NaN || undefined || null || 0 || ' ')=false;
var o={
flag:true}; var test=!!o.flag;// Equivalent to var test=o.flag||false; alert(test);
边栏推荐
- Opencv learning notes - loading and saving images
- Use the object selection tool to quickly create a selection in Adobe Photoshop
- How to make Baidu quickly include its own personal website?
- Ingenious conception - iron death regulatory factor classification and prognosis 6+
- In depth analysis, from ordinary clock system to various time service modes
- Opencv learning notes - cv:: mat class
- Deep parsing and implementation of redis pub/sub publish subscribe mode message queue
- 一文讲透植物内生菌研究怎么做 | 微生物专题
- WPF从零到1教程详解,适合新手上路
- 【2022国赛模拟】摆(bigben)——行列式、杜教筛
猜你喜欢

文本转语音功能上线,可以体验专业播音员的服务,诚邀试用

Opencv learning notes - regions of interest (ROI) and image blending

微医CodeReview工具链

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

WPF from zero to 1 tutorial details, suitable for novices on the road

Dingding, Feishu, and enterprise wechat: different business approaches

【2022国赛模拟】摆(bigben)——行列式、杜教筛
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue

Mlife forum | microbiome and data mining

Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
随机推荐
炒伦敦金短线稳定赚钱技巧?在哪里炒伦敦金安全靠谱?
Dingding, Feishu, and enterprise wechat: different business approaches
Discussion on redis communication protocol
Kubernetes practical skill: entering container netns
Conceptual analysis of DDD Domain Driven Design
[highlights] summary of award-winning activities of Tencent cloud documents
Popular science of data annotation: ten common image annotation methods
文本转语音功能上线,可以体验专业播音员的服务,诚邀试用
[redisson] analysis of semaphore lock source code
Is it safe to apply for new bonds to open an account
Install MySQL in docker and modify my CNF profile
Which commercial insurance endowment insurance is good? Ranking of commercial endowment insurance products in 2022
[2021 techo youth dry goods sorting post, there is always one you are interested in]
Simple and flexible permission design?
Opencv learning notes - loading and saving images
Mlife forum | microbiome and data mining
【2022国赛模拟】摆(bigben)——行列式、杜教筛
Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
Identification of new prognostic DNA methylation features in uveal melanoma by 11+ based on methylation group and transcriptome analysis~
How to check the situation that the national standard platform easygbs equipment video cannot be accessed by grabbing packets?