当前位置:网站首页>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);
边栏推荐
- What are the software prototyping tools?
- Five minutes to develop your own code generator
- Opencv learning notes - loading and saving images
- How can a shell script (.Sh file) not automatically close or flash back after execution?
- Use go to process millions of requests per minute
- Istio FAQ: istio init crash
- The solution of distributed system: directory, message queue, transaction system and others
- [tke] GPU node NVIDIA Tesla driver reinstallation
- How does easygbs, a national standard platform, solve the problem that information cannot be carried across domains?
- Adjustment method of easynvr video platform equipment channel page display error
猜你喜欢
Cloud native database: the outlet of the database, you can also take off

WPF从零到1教程详解,适合新手上路

微医CodeReview工具链

MySQL 外键影响

How can a shell script (.Sh file) not automatically close or flash back after execution?

Mlife forum | microbiome and data mining

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

Opencv learning notes - Discrete Fourier transform
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue

WPF from zero to 1 tutorial details, suitable for novices on the road
随机推荐
Remote terminal RTU slope monitoring and early warning
Conceptual analysis of DDD Domain Driven Design
Single gene pan cancer + simple experiment can be published 7 points+
105. 简易聊天室8:使用 Socket 传递图片
Ingenious conception - iron death regulatory factor classification and prognosis 6+
Is it safe to apply for new bonds to open an account
VaR in PHP_ export、print_ r、var_ Differences in dump debugging
The programmer's graduation project is still bald after a year
RTMP streaming platform easydss video on demand interface search bar development label fuzzy query process introduction
Is it safe to open an account for how many new bonds you can apply for
Installing sqlserver extension PDO of PHP under Linux_ sqlsrv
Cluster control management
Deep parsing and implementation of redis pub/sub publish subscribe mode message queue
99% of the students can't write good code because of this problem!
How to open a new bond? Is it safe to open an account
Design and implementation of high performance go log library zap
一文讲透植物内生菌研究怎么做 | 微生物专题
【数据库】期末复习(计科版)
The world's largest meat processor has been "blackmailed", how many industries will blackmail virus poison?
Making daily menu applet with micro build low code