当前位置:网站首页>ES6解构赋值
ES6解构赋值
2022-06-13 01:08:00 【清华小菜】
ES6允许按照一定的模式,从数组和对象中提取值,对变量进行赋值,这被称为解构
var [a,b,c] = [1,2,3] //a=1.b=2.c=3
这种写法属于‘模式匹配’,只要等号两边的模式相同,左边的变量就会被赋予对应地值
let [aa, [[bb], cc] = [1, [[2], 3]] //aa=1, bb=2, cc=3
let [x, …y] = [1,2,3,4] //x=1,y=[2,3,4]
let [aaa,bbb] = [11] //aaa=11, bbb=undefined
解构值允许指定默认值
let [aaa = true] = [] //aaa = true
let [aa, bb=1] = [11] //aa=11, bb=1
let [xa, xb = 22] = [11, undefined] //xa = 11, xb = 22
es6内部使用严格的相等运算符判断一个位置是否有值,只有当一个成员严格等于undefined时才能使用默认值
默认值可以引用解构赋值的其他变量,但是该变量必须已经声明
let [d=1, e=d] = [] //d=1 e=1
let [g=2, h=g] = [2] //g=2, h=2
let [i=3, j=i] = [11,22] //i=11, j =22
let [k=m, m =1] = [] //报错,m未定义


let obj2 = {
ob:{
st:{
fir:1,
las:5
}
}
}
let {
ob, ob:{
st},ob:{
st:{
fir}}} = obj2 //ob={st: {…}} st={fir: 1, las: 5} fir=1
边栏推荐
- Physical orbit simulation
- What is the difference between pytorch and tensorflow?
- [JS component] customize the right-click menu
- Biological unlocking - Fingerprint entry process
- What is pytorch? Explain the basic concepts of pytorch
- [JS component] calendar
- MySQL performance analysis - explain
- [JS component] create a custom horizontal and vertical scroll bar following the steam style
- Leetcode-12- integer to Roman numeral (medium)
- [latex] insérer une image
猜你喜欢

. The way to prove the effect of throwing exceptions on performance in. Net core
![[latex] insert picture](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[latex] insert picture

What is meebits? A brief explanation
![[Latex] 插入圖片](/img/0b/3304aaa03d3fea3ebb93b0348c3131.png)
[Latex] 插入圖片
![[JS component] floating text](/img/e5/7faad5422bba919bed34e3dbcf7ba0.jpg)
[JS component] floating text

Common skills for quantitative investment - drawing 3: drawing the golden section line

Addition and modification of JPA

Five classic articles worth reading (2)

Matrix fast power

spiral matrix visit Search a 2D Matrix
随机推荐
Aof persistence
leetode. 242. valid Letter heteronyms
Several categories of software testing are clear at a glance
Higherhrnet pre training model -- download from network disk
Three column simple Typecho theme lanstar/ Blue Star Typecho theme
[JS component] custom paging
Wikipedia User Guide
HashSet underlying source code
Breadth first search for node editor runtime traversal
What kind of experience is it to be a software test engineer in a state-owned enterprise: every day is like a war
Characteristics of transactions -- atomicity (implementation principle)
Tree - delete all leaf nodes
[003] embedded learning: creating project templates - using stm32cubemx
About_ int128
Today's sleep quality record 74 points
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of BBI, MTM, obv, CCI and priceosc indicators
Undirected graph -- computing the degree of a node in compressed storage
Kotlin coroutine suspend function suspend keyword
Leetcode-18- sum of four numbers (medium)
MySQL performance analysis - explain