当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
The gun startles the dragon, and the crowd "locks" Zhou Zhi
Use load_ decathlon_ Datalist (Monai) fast loading JSON data
MONAI版本更新到 0.9 啦,看看有什么新功能
[pro test feasible] error while loading shared libraries solution
uniCloud
南航 PA3.1
Operation method of Orange Pie orangepi 4 lts development board connecting SATA hard disk through mini PCIe
Gym installation pit records
单调性约束与反单调性约束的区别 monotonicity and anti-monotonicity constraint
[système recommandé 01] rechub
Schnuka: machine vision positioning technology machine vision positioning principle
Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
Transaction rolled back because it has been marked as rollback-only解决
Realize ray detection, drag the mouse to move the object and use the pulley to scale the object
I plan to take part in security work. How about information security engineers and how to prepare for the soft exam?
Transaction rolled back because it has been marked as rollback only
[OneNote] can't connect to the network and can't sync the problem
How much review time does it usually take to take the intermediate soft exam?
Use of dotween
Multisim -- software related skills