当前位置:网站首页>ES6之解构函数
ES6之解构函数
2022-04-23 05:37:00 【不知言】
对象解构-根据key检索取值
let demo = {
name: 'dao',
value: 0
}
// 基础用法
let {
name, value } = demo;
console.log(name);
console.log(value);
// 赋值用法
let {
name: localName } = demo;
console.log(localName);
// 默认值,在demo中找不到时,默认赋值为0
let {
type: localType = 0 } = demo;
// 嵌套解构
let node = {
parent: {
child: {
name: 'dom',
value: 'class'
}
}
}
let {
parent: {
child } } = node;
console.log(child.name);
console.log(child.value);
数组解构-根据位置(索引)检索取值
let demo = ['red', 'green', 'blue'];
// 取值和默认值
let [, second,,four = 'yellow'] = demo;
console.log(second);
console.log(four);
// 值交换
let x = 1;
let y = 2;
[x, y] = [y, x];
console.log(x);
console.log(y);
// 嵌套数组
let node = [[...demo], [1, 2, 3]];
let [[a], [b]] = node;
console.log(a);
console.log(b);
// 剩余项
let [first, ...rest] = demo;
console.log(first);
console.log(rest.length);
console.log(rest[0]);
混合解构
// 在以上的基础上,对数组与对象的复合数据结构进行结构
let node = {
parent: {
child: {
name: 'dom',
value: 'class'
}
},
type: [1, 2, 3]
}
let {
parent: {
chilid }, type: [first] } = node;
console.log(child.name);
console.log(first);
版权声明
本文为[不知言]所创,转载请带上原文链接,感谢
https://blog.csdn.net/WangYanWenXin/article/details/124344868
边栏推荐
- STL function library
- solidity合约DOS攻击
- Three methods of list rendering
- Similarities and differences between vector and array (notes)
- catkin_package到底干了什么
- Knowledge of egg testing -- mock, Supertest, coffee
- Phlli in a VM node
- Cmake basic tutorial (39) pkgconfig
- Pol / select / EPO
- The main difference between pointer and reference
猜你喜欢
Traversal array, object parent-child communication props / $emit
On the use of constant pointer and pointer constant -- exercise (record)
QT displays the specified position and size of the picture
如果我是pm之 演出电影vr购票展示
Uncle wolf is looking for a translator -- Plato -- ongoing translation
Use of ES6 array
what is wifi6?
STL learning notes 0x0001 (container classification)
[untitled] Notepad content writing area
QT drawpixmap and DrawImage blur problem
随机推荐
C, class library
提升独立站转化率攻略 | 挽回弃购用户
可執行程序執行流程
Excel sets row and column colors according to cell contents
Error handling mechanism of the strongest egg framework in history
windows连接mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)
Executable program execution process
Markdown syntax support test
catkin_package到底干了什么
Edit, cancel, pull up menu
3d slicer中拉直体的生成
Basic knowledge of redis
Note: unordered_ Understanding and use of map
Phlli in a VM node
Introduction to data security -- detailed explanation of database audit system
The QT debug version runs normally and the release version runs crash
CMake基础教程(39)pkgconfig
‘EddiesObservations‘ object has no attribute ‘filled‘
五一劳动节期间什么理财产品会有收益?
弘玑Cyclone RPA为国金证券提供技术支撑,超200个业务场景实现流程自动化