当前位置:网站首页>ES6--解构赋值
ES6--解构赋值
2022-07-27 17:48:00 【小气鬼SYQ】
<script>
// 传统数组赋值
let arr = ["前端", "后端", "大数据"]
let q = arr[0]
let h = arr[1]
let d = arr[2]
console.log(q, h, d);
// es6解构赋值--数组解构赋值
let [a, b, c] = arr
console.log(a, b, c);
// 传统对象赋值
let obj = { name: "张三", age: 20 }
let name1 = obj.name;
let age1 = obj.age
console.log(name1, age1);
// es6解构赋值--对象解构赋值
let { name2, age2 } = obj
console.log(name2, age2);
// 赋值和顺序无关,属性名和变量名必须一致
// let { name, age, data: { msg, status } } = { name: "张三", age: 20, data: { msg: "ok", status: 200 } }
// console.log(name, age, msg, status);
// 给解构出来的变量修改名字
// let { name: uname, age: uage, data: { msg: umsg, status: ustatus } } = { name: "张三", age: 20, data: { msg: "ok", status: 200 } }
// console.log(uname, uage, umsg, ustatus);
// 给解构出来的变量添加默认值
// 当对象里的属性值是undefined,或者没有这个属性,这时候就给变量赋值成默认值
let { name: uname = "李四", age3 = 50, data: { msg: umsg, status: ustatus } } = { name: undefined, age3: undefined, data: { msg: "ok", status: 200 } }
console.log(uname, age3, umsg, ustatus);
</script>边栏推荐
- PMP每日一练 | 考试不迷路-7.27(包含敏捷+多选)
- Codeworks 5 questions per day (average 1500) - day 24
- Unified Modeling Language (UML) specification
- MVCC的底层原理
- 2019年全球半导体营收同比下滑12%,中国市场份额第一
- Chapter 2 Introduction
- Technology sharing | how to do Assertion Verification in interface automated testing?
- [Redis] Redis穿透、雪崩和击穿
- TS2532: Object is possibly ‘undefined‘
- 【OpenBMC 系列】4.启动流程 使用qume模拟ast2600-evb
猜你喜欢

Capacitance in series and in parallel and capacitance in series and balance resistance

Product Manager: check where there is an error prompt of "system exception" on the offline

'vite' is not an internal or external command, nor is it a runnable program or batch file

Codeworks 5 questions per day (average 1500) - day 24

Use cpolar to build a business website (5)
![22 year PMP test [Quanzhen agile test]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
22 year PMP test [Quanzhen agile test]

Technology sharing | how to do Assertion Verification in interface automated testing?

#yy关于鱼的英文学习

C background GC cause and effect

What does bus mean
随机推荐
C243: examination ranking
Codeworks 5 questions per day (average 1500) - day 24
MVCC的底层原理
如何运行 kevinchappell / formBuilder
10.31 extended configuration of static route
Program design Comprehensive Experiment III
Product Manager: check where there is an error prompt of "system exception" on the offline
C# 后台GC 的前因后果
TS2532: Object is possibly ‘undefined‘
聊聊 Redis 是如何进行请求处理
【OpenBMC 系列】4.启动流程 使用qume模拟ast2600-evb
Common errors reported by pytorch
PC Museum (3) MITs Altair 8800
GLTF模型添加关节控制
uva1421
C # network application programming, experiment 2: IP address translation and domain name resolution exercises
由单片机XTALIN引脚和XTALOUT引脚导出的对晶体震荡电路的深入理解
产品经理:排查下线上哪里冒出个“系统异常”的错误提示
An in-depth understanding of crystal oscillation circuit derived from xtalin pin and xtalout pin of single chip microcomputer
C191: password compilation