当前位置:网站首页>Deconstruction assignment of variables
Deconstruction assignment of variables
2022-06-25 14:17:00 【Rabbit^-^】
Deconstruction and assignment of variables
To reduce the definition of variables , And it's easier to extract values from arrays or objects ,ES6 Deconstruction assignment
Deconstruction and assignment of arrays
let[a,b,c]=[1,2,3]
console.log(a)//1
console.log(b)//2
console.log(c)//3
You can omit variables
Can be used with the extension operator
let[a,...b]=[1,2,3,4,5]
console.log(a)//1
console.log(b)//[2, 3, 4, 5]
You can define default values
let[a,b,c=0]=[1,2]
console.log(a)//1
console.log(b)//2
console.log(c)//0
Other variables to which the deconstruction assignment can be applied
let[a,c=a]=[1]
console.log(a)//1
console.log(c)//1
Pay attention to the situation
1. The types before and after the deconstruction assignment correspond to
let[a]=null// Report errors
let[a]={}// Report errors
2.undefined Default values cannot be replaced
let[a=0]=[null]//null
let[a=0]=[undefined]//0
3. Reference extension operators and other variables assigned by deconstruction , You can't write backwards
let[...a,b]=[1,2,3,4]// Report errors
let[c=a,a=1]=[]// Report errors
Object and deconstruction assignment
Basic usage
let{name,sex} ={name:" Zhang Liang ",sex:" male "}
console.log(name)// Zhang Liang
console.log(sex)// male
Aliasing objects
let{name:a,sex:b} ={name:" Zhang Liang ",sex:" male "}
console.log(a)// Zhang Liang
console.log(b)// male
You can define default values
let{name:a,sex:b,age=22} ={name:" Zhang Liang ",sex:" male "}
console.log(age)//22
Resolution of nested objects
var {name,mon:[a,b,c]}={name:" Zhang ",mon:[2000,3000,1000]}
console.log(name)// Zhang
console.log(a)//2000
console.log(b)//3000
console.log(c)//1000
Deconstruction and assignment of strings
Parse the value of the string
var [...arr]="hello"
console.log(arr)//[h,e,l,l,o]
Resolve the properties and methods under the object
var {length}="hello"
console.log(length)//5
Parameter deconstruction assignment under function
When parameters are deconstructed for an array
function fun([a,b,c]){
console.log(a)
console.log(b)
console.log(c)
}
fun(1,2,3)
When a parameter is deconstructed for an object
function fun1({name:a,age:b}){
console.log(a+","+b)
}
fun1({name:"z",age:23})
Be careful : When the value after the deconstruction assignment is of other types , First consider whether this type can be converted into an object , Otherwise, it cannot be parsed
New attribute method under reference type
Digital extension
0b10101// Define binary
0o767// octal o It can be in both case
Properties and methods
parseInt() Convert to an integer
parseFloat() Convert to floating point number
isNaN() Judge whether it's a number
isFinite() Judge if it's a finite number
isInteger() Is it an integer // Integer returns true
Mathematical expansion
trunc() Remove the decimal part of a number
sign() Judge whether a number is a positive number 、 negative 、 It's still zero
return +1 Positive numbers return -1 negative
return +0 Positive zero return -0 Negative zero
return NaN Other values
cbrt() Calculate the cube root of a number
imul() Returns the product of two numbers , Include a plus or minus sign
hypot() Returns the square root of the sum of squares of parameters
signbit() Judge positive and negative numbers , Returns a Boolean value , Returns a negative number true
console.log(Math.signbit(-5))//true
console.log(Math.signbit(5))//false
console.log(Math.signbit(+0))//false
console.log(Math.signbit(-0))//true
console.log(Math.signbit("add"))//flase
Index of operation :2**3// by 8,2 multiply 3 All over =>2*2*2
边栏推荐
- 使用KVM虚拟化部署EVE-NG
- 用NumPy实现神经网络(Mysteries of Neural Networks Part III)
- 哈希錶、哈希沖突
- 112页机器学习-数学基础回顾.pptx
- Tencent cloud builds a Socks5 multi IP proxy server to realize the perfect building of a game with a single window and a single IP. Tutorial attached tool "suggestions collection"
- How to choose a technology stack for web applications in 2022
- Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
- 广发易淘金和同花顺哪个更好,更安全一些
- 分类器与cross entropy loss函数
- Complete and detailed compilation of experimental reports
猜你喜欢

多台云服务器的 Kubernetes 集群搭建

让PyTorch训练速度更快,你需要掌握这17种方法
![[proteus simulation] 51 MCU +ds1302+lcd1602 display](/img/02/9644415b72c330afa15060d81d4cbc.png)
[proteus simulation] 51 MCU +ds1302+lcd1602 display

shell 字符串变量

算力&NFT交易平台F3.xyz旗下独家权益NFT项目Hash Eagle将盛大发行

Windows下MySQL的安装和删除

Graph contractual learning with augmentations

两种方法实现pycharm中代码回滚到指定版本(附带截图展示)

Application of tactile intelligent sharing-rk3568 in financial self-service terminal

'NVIDIA SMI' is not an internal or external command, nor is it a runnable program or batch file
随机推荐
国信证券股票账户开户安全吗?请问。
测一测你的挣钱能力有多强?未来的你将从事什么职业?
Qt内存映射
Kubernetes cluster construction of multiple ECS
shell 字符串变量
shell 变量 入门
【世界历史】第一集——石器时代的人们
当了六年程序员第一次搞懂微服务架构的数据一致性,真不容易
Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
Turtlebot+lms111+gmapping practice
论文阅读:Graph Contrastive Learning with Augmentations
Logistic Regression VS Linear Regression
Is it safe to open an account with tongdaxin stock?
BACnet gateway bl103 for building automation
API encapsulation of uniapp applet
Is qiniu regular? Is it safe to open a stock account?
Les neuf caractéristiques de la parole et les neuf temps en anglais
Rust, le meilleur choix pour un programmeur de démarrer une entreprise?
Encapsulating functions and event handling
Error1822 and error1824 are displayed in the database