当前位置:网站首页>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
边栏推荐
- Several categories of software testing are clear at a glance
- Pipeline流水线项目构建
- Traditional machine learning classification model predicts the rise and fall of stock prices
- The tle4253gs is a monolithic integrated low dropout tracking regulator in a small pg-dso-8 package.
- Kotlin collaboration, the life cycle of a job
- FLIP动画实现思路
- Kotlin coroutine suspend function suspend keyword
- Canvas game 2048 free map size
- How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
- Kotlin coroutine withcontext switch thread
猜你喜欢

Liu Hui and introduction to nine chapter arithmetic and island arithmetic

刘徽与《九章算术》《海岛算经》简介

spiral matrix visit Search a 2D Matrix

How many steps are appropriate for each cycle of deep learning?

5G工业网关在煤矿行业的应用优势
![[JS component] dazzle radio box and multi box](/img/2a/00620bee312972db93e1db4313385f.jpg)
[JS component] dazzle radio box and multi box

Quantitative investment traditional index investment decision vs Monte Carlo simulation method

How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income

Illustrator tutorial, how to add dashes and arrows in illustrator?

Tangent and tangent plane
随机推荐
[latex] insert picture
redis
[server data recovery] successful cases of data loss recovery during data migration between storage servers
Get preview of precast body
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of DBCD, ROC, vroc, Cr and psy index strategy income
How to choose stocks? Which indicator strategy is reliable? Quantitative analysis and comparison of strategic returns of vrsi, bbiboll, WR, bias and RSI indicators
[003] embedded learning: creating project templates - using stm32cubemx
Common skills for quantitative investment - drawing 2: drawing the moving average
Bubble sort - alternate sort at both ends
Quantitative investment traditional index investment decision vs Monte Carlo simulation method
Leetcode-19- delete the penultimate node of the linked list (medium)
Tangent and tangent plane
Three threads print digital demo alternately
Addition and modification of JPA
Quick power explanation
Binary tree traversal - recursive and iterative templates
Kotlin coroutine withcontext switch thread
Opencv desaturation
[JS component] floating text
[JS component] calendar