简体   繁体   English

为什么此对象在for-of循环中进行销毁不起作用?

[英]Why this object destructuring in for-of loop doesn't work?

Basically the following code doesn't work and I don't understand why. 基本上,以下代码不起作用,我也不明白为什么。 in the nested loop, I have a variable called "children" that is an array containing objects with fields called children (like in a DOM tree for instance). 在嵌套循环中,我有一个名为“ children”的变量,该变量是一个数组,其中包含带有称为children的字段的对象(例如,在DOM树中)。 But it looks like their is a name conflict when I try to desctructure each element of the array. 但是,当我尝试分解数组的每个元素时,它们似乎是名称冲突。 Since I'm using let they should have different scope (at least, the destructured variable should hide the "parent" one). 由于我正在使用let,它们应该具有不同的作用域(至少,解构后的变量应隐藏“父”变量)。 It's likely that I'm wrong but I would like to understand why. 我可能错了,但我想了解原因。

 let o = { children: [ {children: [{}, {}, {}]}, {children: [{}, {}, {}]} ] }; for (let {children} of o.children) { console.log(children) for (let {children} of children) { console.log(children) } } 

By taking this statement, 通过采取这种说法,

for (let {children} of children)

you try to create a new local variable and take the same named variable as source for getting the elements for the new variable. 您尝试创建一个新的局部变量,并以相同的命名变量作为源来获取新变量的元素。

This does not work, because there is no distinction between both variables. 这是行不通的,因为两个变量之间没有区别。

It would work, if you rename the target or source variable. 如果重命名目标或源变量,它将起作用。

 let o = { children: [ { children: [{ children: [{}, {}, {}] }, { children: [{}, {}, {}] }] }, { children: [{ children: [{}, {}, {}] }, { children: [{}, {}, {}] }] }, ] }; for (let { children } of o.children) { console.log(children) for (let { children: c } of children) { // rename here console.log(c) } } 

You're attempting to get a property children from the wrong place. 您正试图从错误的地方找寻财产children Remove the second for loop or rename your variable: 删除第二个for循环或重命名您的变量:

 let o = { children: [ {children: [{}, {}, {}]}, {children: [{}, {}, {}]} ] }; for (let { children } of o.children) { console.log(children); } 

Or: 要么:

 let o = { children: [ {children: [{children: "child"}, {children: "child"}, {children: "child"}]}, {children: [{children: "child"}, {children: "child"}, {children: "child"}]} ] }; for (let { children } of o.children) { console.log(children); for (let { children: childrenB } of children) { console.log(childrenB); } } 
 .as-console-wrapper { max-height: 100% !important; top: auto; } 

The problem is in the nested for..of . 问题出在for..of的嵌套中。 See the line 看线

for (let {children} of children){...}

Here you are trying to get children property of children which doesnot make sense. 在这里,你试图让children的财产children这亘古不变的是有意义的。 Due to let {children} it creates a local variable children which is currently no initialized. 由于let {children}它创建了一个局部变量children ,该children变量当前尚未初始化。 Now the second children doesnot refer to parent array. 现在第二个children不列入参考父阵列。 But it refers to uninitialized variable in local scope. 但是它指的是本地范围内的未初始化变量。

The code in this case will throw a reference error. 这种情况下的代码将引发参考错误。

"Uncaught ReferenceError: Cannot access 'children' before initialization" “未捕获的ReferenceError:初始化之前无法访问'子级'”

Consider an object with name children and it property name is also children .You are doing same as below. 考虑一个名称为children的对象,它的属性名称也为children 。您的操作如下。

 let children = {children:'something'} { let {children} = children; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM