当前位置:网站首页>变量的解构赋值
变量的解构赋值
2022-07-07 08:49:00 【wax9092】
前言:
旧书不厌百回读,熟读精思子自知
参考文档:变量的解构赋值 · 语雀
解构异常
1.数组解构报错 --- 不是可遍历的结构
let [a] = 1; //Uncaught TypeError: 1 is not iterable
let [b] = false;//Uncaught TypeError: false is not iterable
let [c] = NaN;//Uncaught TypeError: NaN is not iterable
let [d] = undefined;//Uncaught TypeError: undefined is not iterable
let [e] = null;//Uncaught TypeError: null is not iterable
let [f] = {};//Uncaught TypeError: {} is not iterable2.数组解构失败 --- 匹配不到变量,值等于undefined
let [a, b] = [];
console.log("a=>",a) //a=> undefined
console.log("b=>",b) //b=> undefined3.对象解构报错
let {b} = undefined;//Uncaught TypeError: Cannot destructure property 'foo' of 'undefined' as it is undefined.
let {e} = null;// Uncaught TypeError: Cannot destructure property 'e' of 'null' as it is null.4.对象解构失败 --- 匹配不到变量,值等于undefined
let {a} = 1; //a=> undefined
let {b} = false;//b=> undefined
let {c} = NaN;//c=> undefined
let {f} = {};//f=> undefined解构重命名
对象重命名:使用场景同一作用域解构同名变量时(let&const);
// 对象变量重命名
let {name:first,tag} = {name:"555",tag:"1222"};
console.log(name) //
console.log(first)//555
console.log(tag) // 1222
let {name:second,tag:three} = {name:"666",tag:"777"};
console.log(second)//666
console.log(three) // 77解构赋值
设置默认值注意事项:
1.对象设置默认值报错
let {one = "677"} = null; //Uncaught TypeError: Cannot read properties of null (reading 'one')
let {two = "677"} = undefined; //Uncaught TypeError: Cannot read properties of null (reading 'two')
let { data = []} = null;// Cannot read properties of null (reading 'data')2.数组设置默认值报错
如果是解构报错,则无法进行设置默认值
let [a] = 1; //Uncaught TypeError: 1 is not iterable
let [b] = false;//Uncaught TypeError: false is not iterable
let [c] = NaN;//Uncaught TypeError: NaN is not iterable
let [d] = undefined;//Uncaught TypeError: undefined is not iterable
let [e] = null;//Uncaught TypeError: null is not iterable
let [f] = {};//Uncaught TypeError: {} is not iterable总结:
由于解构会有异常情况, 则使的解构赋值存在一定的局限性;
// 假设请求接口获取一个列表数据(应该是一个数组),,返回结构如下:
const data = {
tableList:[],
totalCount:0
}
//使用解构复制
let { tableList = []} = data;
console.log("tableList",tableList) // []
//但是难免会存在null情况
const data2 = {
result:null,
totalCount:0
}
let { result = []} = data2;
console.log("result",result) // result null 设置默认值失效
// 因为不符合数据结构,得在定一个补丁
const last = result || [];
console.log("last",last) // result null 设置默认值失效
//再来看一个情况 ,没有数据返回的是 null
const { list = []} = null;
console.log("list",list) //Uncaught TypeError: Cannot read properties of null (reading 'list'
// 直接解构报错了,此时解构的局限性就体现出来了。由此,解构赋值的时候,一定要避免解构报错。
边栏推荐
- Unity determines whether the mouse clicks on the UI
- Unity script visualization about layout code
- 【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
- [dai6] mirror image of JZ27 binary tree
- 深入理解Apache Hudi异步索引机制
- seata 1.3.0 四种模式解决分布式事务(AT、TCC、SAGA、XA)
- 2022年上半年5月网络工程师试题及答案
- [actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
- Socket communication principle and Practice
- [homework] 2022.7.6 write your own cal function
猜你喜欢

“梦想杯”2017 年江苏省信息与未来小学生夏令营 IT 小能手 PK 之程序设计试题

Mendeley -- a free document management tool that automatically inserts references into papers

【机器学习 03】拉格朗日乘子法

中级软件评测师考什么

无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。

香橙派OrangePi 4 LTS开发板通过Mini PCIE连接SATA硬盘的操作方法

Leetcode-560: subarray with sum K

MONAI版本更新到 0.9 啦,看看有什么新功能

SQL Server 知识汇集9 : 修改数据

July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
随机推荐
What is an intermediate network engineer? What is the main test and what is the use?
Arduino receives and sends strings
China Southern Airlines pa3.1
枪出惊龙,众“锁”周之
[dai6] mirror image of JZ27 binary tree
【实战】霸榜各大医学分割挑战赛的Transformer架构--nnFormer
[homework] 2022.7.6 write your own cal function
How embedded engineers improve work efficiency
软考中级电子商务师含金量高嘛?
CSAPP Bomb Lab 解析
Unity determines whether the mouse clicks on the UI
SQL Server knowledge collection 11: Constraints
Leetcode-304: two dimensional area and retrieval - matrix immutable
在线硬核工具
南航 PA3.1
Mendeley -- a free document management tool that automatically inserts references into papers
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
中级网络工程师是什么?主要是考什么,有什么用?
Use of dotween
使用Tansformer分割三维腹部多器官--UNETR实战