当前位置:网站首页>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 iterable
2. 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=> undefined
3. 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=> undefined
Deconstruction 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) // 77
Deconstruct 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 iterable
summary :
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 .
边栏推荐
- 软考中级,软件设计师考试那些内容,考试大纲什么的?
- [système recommandé 01] rechub
- When do you usually get grades in the soft exam? Online pedaling?
- The gun startles the dragon, and the crowd "locks" Zhou Zhi
- Ffmpeg record a video command from RTSP
- P2788 math 1 - addition and subtraction
- Wallhaven壁纸桌面版
- What is an intermediate network engineer? What is the main test and what is the use?
- Unity downloads files through the server address
- [installation system] U disk installation system tutorial, using UltraISO to make U disk startup disk
猜你喜欢
1324: [example 6.6] integer interval
1323: [example 6.5] activity selection
软考信息处理技术员有哪些备考资料与方法?
多线程-异步编排
July 10, 2022 "five heart public welfare" activity notice + registration entry (two-dimensional code)
单调性约束与反单调性约束的区别 monotonicity and anti-monotonicity constraint
高级软考(网络规划设计师)该如何备考?
CAS机制
软考中级,软件设计师考试那些内容,考试大纲什么的?
Seata 1.3.0 four modes to solve distributed transactions (at, TCC, Saga, XA)
随机推荐
A case of compiling QT file qmake compiling script
Openinstall and Hupu have reached a cooperation to mine the data value of sports culture industry
Summary of router development knowledge
Get pictures through opencv, change channels and save them
QT document
2022.7.6DAY598
seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
软考一般什么时候出成绩呢?在线蹬?
CAS mechanism
JS implementation chain call
Realize ray detection, drag the mouse to move the object and use the pulley to scale the object
SQL Server 知识汇集11 : 约束
Static semantic check of clang tidy in cicd
软考中级有用吗??
[pro test feasible] error while loading shared libraries solution
深入理解Apache Hudi异步索引机制
2021 summary and 2022 outlook
A simple example of delegate usage
2022年上半年5月网络工程师试题及答案
Records on the use of easyflash v3.3