当前位置:网站首页>Deconstruction and assignment of variables
Deconstruction and assignment of variables
2022-07-07 10:58:00 【wax9092】
Preface :
Old books never tire of being read again and again , Read carefully, think carefully, and know yourself
Reference documents : Deconstruction and assignment of variables · Language sparrow
Deconstruction anomaly
1. Array deconstruction error --- Not ergodic structures
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. Array deconstruction failed --- Cannot match variable , The value is equal to undefined
let [a, b] = [];
console.log("a=>",a) //a=> undefined
console.log("b=>",b) //b=> undefined3. Object deconstruction error
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. Object deconstruction failed --- Cannot match variable , The value is equal to undefined
let {a} = 1; //a=> undefined
let {b} = false;//b=> undefined
let {c} = NaN;//c=> undefined
let {f} = {};//f=> undefinedDeconstruction rename
Object renaming : When using the same scope of the scene to deconstruct variables with the same name (let&const);
// Rename object variables
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) // 77Deconstruct assignment
Precautions for setting default values :
1. Object setting default value error
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. An error is reported when setting the default value of the array
If it is a deconstruction error , The default value cannot be set
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 iterablesummary :
Because there will be exceptions in Deconstruction , Then the deconstruction assignment of has certain limitations ;
// Suppose the request interface gets a list data ( It should be an array ),, The return structure is as follows :
const data = {
tableList:[],
totalCount:0
}
// Use deconstruction to copy
let { tableList = []} = data;
console.log("tableList",tableList) // []
// But it will inevitably exist null situation
const data2 = {
result:null,
totalCount:0
}
let { result = []} = data2;
console.log("result",result) // result null Setting the default value is invalid
// Because it does not conform to the data structure , We have to order a patch
const last = result || [];
console.log("last",last) // result null Setting the default value is invalid
// Let's take another look , No data is returned null
const { list = []} = null;
console.log("list",list) //Uncaught TypeError: Cannot read properties of null (reading 'list'
// Direct deconstruction reports an error , At this point, the limitations of deconstruction are reflected .thus , When deconstructing assignment , Be sure to avoid deconstruction errors .
边栏推荐
- Use load_ decathlon_ Datalist (Monai) fast loading JSON data
- shardingsphere分库分表示例(逻辑表,真实表,绑定表,广播表,单表)
- Find the root of equation ax^2+bx+c=0 (C language)
- 枪出惊龙,众“锁”周之
- P1031 [noip2002 improvement group] average Solitaire
- Mysql的json格式查询
- Unity script generates configurable files and loads
- TypeScript 接口继承
- CSAPP bomb lab parsing
- Simple and easy to modify spring frame components
猜你喜欢

【OneNote】无法连接到网络,无法同步问题

Use load_ decathlon_ Datalist (Monai) fast loading JSON data
![[actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer](/img/de/0cf12132216ffbde896a7b12022184.png)
[actual combat] transformer architecture of the major medical segmentation challenges on the list --nnformer
![[système recommandé 01] rechub](/img/92/c14c867247d3a042c69b5ed0091fbe.png)
[système recommandé 01] rechub

Qtcreator sets multiple qmake

Idea shortcut keys

Différences entre les contraintes monotones et anti - monotones

ArrayList thread insecurity and Solutions

Mpx 插件

What are the test preparation materials and methods for soft exam information processing technicians?
随机推荐
Unable to open kernel device '\.\vmcidev\vmx': operation completed successfully. Reboot after installing vmware workstation? Module "devicepoweron" failed to start. Failed to start the virtual machine
PHP \ newline cannot be output
【推荐系统 02】DeepFM、YoutubeDNN、DSSM、MMOE
2022年7月10日“五心公益”活动通知+报名入口(二维码)
Applet jump to H5, configure business domain name experience tutorial
Cmake learning manual
[OneNote] can't connect to the network and can't sync the problem
CC2530 ZigBee iar8.10.1 environment construction
CSAPP Bomb Lab 解析
Is the soft test intermediate useful??
香橙派OrangePi 4 LTS开发板通过Mini PCIE连接SATA硬盘的操作方法
Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!
Basic introduction of yarn and job submission process
Shardingsphere sub database and table examples (logical table, real table, binding table, broadcast table, single table)
Mendeley--免费的文献管理工具,给论文自动插入参考文献
2022年上半年5月网络工程师试题及答案
Deep understanding of Apache Hudi asynchronous indexing mechanism
路由器开发知识汇总
IDEA快捷键大全
ThreadLocal is not enough