current position:Home>Common special characters in JS and TS
Common special characters in JS and TS
2022-06-24 12:40:08【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);
copyright notice
author[Star River dream~],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/175/202206241026151363.html
The sidebar is recommended
- Tencent Youtu, together with Tencent security Tianyu and wechat, jointly launched an infringement protection scheme
- Fbnet/fbnetv2/fbnetv3: Facebook's lightweight network exploration in NAS | lightweight network
- A flaw in R markdown: folders cannot have Chinese
- Getting started with scrapy
- Detailed explanation of the execution order of the expression and loop body in the for loop
- Remote terminal RTU slope monitoring and early warning
- Jupyter notebook service installation and startup
- About Adobe Photoshop adjusting selection
- Kubernetes practical skills: use cert manager to issue free certificates for DNSPod domain names
- In depth analysis, from ordinary clock system to various time service modes
guess what you like
What are the software prototyping tools?
[tke] GPU node NVIDIA Tesla driver reinstallation
A good habit that makes your programming ability soar
Google hacking search engine attack and Prevention
Data stack technology sharing: open source · data stack - extend flinksql to realize the join of flow and dimension tables
Pinduoduo press the user accelerator key
Installing sqlserver extension PDO of PHP under Linux_ sqlsrv
Go basic series | 1 Leading
Node cache vs browser cache
Istio practical skills: implement header based authorization
Random recommended
- [Tencent cloud 618 countdown!] Promotion strategy of the promotion activities
- Simple and flexible permission design?
- Making daily menu applet with micro build low code
- 99% of the students can't write good code because of this problem!
- Istio practical skills: using prism to construct multi version test services
- Kubernetes practical technique: setting kernel parameters for pod
- A scheme for crawlers to collect public opinion data
- Essential key steps in the construction of e-commerce live broadcast source code
- How do websites and we media tap user needs? Deeply expose the secrets behind the keywords!
- From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
- Continuous testing | key to efficient testing in Devops Era
- It's settled! Bank retail credit risk control just does it!
- Encapsulate the method of converting a picture file object to Base64
- The pod is evicted due to insufficient disk space of tke node
- Post processing - deep camera deformation effects
- Tencent released credit risk control results safely: it has helped banks lend more than 100 billion yuan
- Interesting erasure code
- Five minutes to develop your own code generator
- How to make secruecrt more productive
- About me, a 19 line programmer
- Kubernetes practical skill: entering container netns
- [programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
- Design and implementation of high performance go log library zap
- Ghost, a synonym for blog system
- Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
- IOMMU (VII) -vfio and mdev
- [highlights] summary of award-winning activities of Tencent cloud documents
- The programmer's graduation project is still bald after a year
- How to solve the problem that MBR does not support partitions over 2T, and lossless transfer to GPT
- Another prize! Tencent Youtu won the leading scientific and technological achievement award of the 2021 digital Expo
- Use txvideoeditor to add watermark and export video card at 99%? No successful failed callback?
- Reset the password, and the automatic login of the website saved by chrome Google browser is lost. What is the underlying reason?
- [log service CLS] Tencent cloud log service CLS accesses CDN
- [live broadcast of celebrities] elastic observability workshop
- Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
- SMS SMS
- WPF from zero to 1 tutorial details, suitable for novices on the road
- Dingding, Feishu, and enterprise wechat: different business approaches
- [2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
- How to do research on plant endophytes? Special topic on Microbiology