简体   繁体   English

for 循环用于在其中声明变量但不在全局范围内

[英]The for loop works on declaring the variable inside it but not in the global scope

when I declared the variable that contains the li outside the for loop, the loop only printed one list item.当我在 for 循环外声明包含 li 的变量时,循环只打印了一个列表项。 I read about that issue and found that two identical nodes can't be present in the same place simultaneously, so the loop deletes the created li every time to add the new one (which results at the end in only one list item)我读到了那个问题,发现两个相同的节点不能同时出现在同一个地方,所以循环每次都会删除创建的 li 以添加新的(最后只产生一个列表项)

But on declaring the variable in the for loop, the code worked and 4 list items were created.但是在 for 循环中声明变量时,代码有效并创建了 4 个列表项。 I want to understand what happened programmatically when I declared the variable inside the loop.我想了解当我在循环内声明变量时以编程方式发生了什么。

I then tried to print the variable of the list item in the console, and it printed the 4 list items.然后我尝试在控制台中打印列表项的变量,它打印了 4 个列表项。 Does that mean the when the loop runs each time, it just updates the value of the listItem variable to include one more list item at a time?这是否意味着每次循环运行时,它只是更新 listItem 变量的值以一次包含一个列表项?

But how doesn't a redeclaration of the variable take place?但是如何不重新声明变量呢?

The code:代码:

let theMenu = document.createElement("ul");
for (let i = 0 ; i <= 3; i++) {
    let listItem = document.createElement("li");
    theMenu.append(listItem);
} 

In the code you provided, the listItem variable is declared inside the loop.在您提供的代码中,listItem 变量是在循环内声明的。 This means that it is a new variable that is created every time the loop iterates.这意味着它是每次循环迭代时创建的新变量。 When the loop iterates, the listItem variable is created, a new li element is created, and then the listItem variable is appended to theMenu.当循环迭代时,创建 listItem 变量,创建一个新的 li 元素,然后将 listItem 变量附加到 theMenu。 When the loop moves on to the next iteration, the listItem variable is created again and the process repeats.当循环进入下一次迭代时,将再次创建 listItem 变量并重复该过程。

If you had declared the listItem variable outside the loop, it would only have been created once, and every time the loop tried to append it to theMenu, it would overwrite the previous li element that was appended.如果您在循环外声明了 listItem 变量,它只会被创建一次,并且每次循环尝试将它追加到 theMenu 时,它都会覆盖之前追加的 li 元素。 This is why only one list item was created when you declared the variable outside the loop.这就是为什么在循环外声明变量时只创建一个列表项的原因。

The reason that the code works when the listItem variable is declared inside the loop is that a new variable is created every time the loop iterates, so there is no overwriting of the previous li element.当在循环内声明 listItem 变量时代码起作用的原因是每次循环迭代时都会创建一个新变量,因此不会覆盖先前的 li 元素。 This allows all four li elements to be created and appended to theMenu.这允许创建所有四个 li 元素并将其附加到 Menu。

An element can only appear in a document once.一个元素只能在一个文档中出现一次。

If you append an element that is already somewhere in the document then it will be moved not cloned.如果您append一个已经在文档中某处的元素,那么它将被移动而不是克隆。

If you place the line let listItem = document.createElement("li");如果你放置一行let listItem = document.createElement("li"); outside the loop then you create one list item, then append it to the document four times (moving it the second and subsequent times).在循环之外,然后创建一个列表项,然后将其附加到文档中四次(第二次和后续移动)。

If you keep that line inside the loop then you create four list items.如果您将该行保留在循环中,那么您将创建四个列表项。

Here's what happens during the for...loop iterations on these two cases:以下是这两种情况下 for...loop 迭代过程中发生的情况:

  1. The listItem is a global variable, therefore the menuItem keeps a single reference to one object. listItem 是一个全局变量,因此 menuItem 保持对一个对象的单一引用。

在此处输入图像描述

  1. The listItem is a local variable, recreated from scratch on each iteration, therefore the menuItem keeps 3 different object references: listItem 是一个局部变量,在每次迭代时从头开始重新创建,因此 menuItem 保留 3 个不同的对象引用:

在此处输入图像描述

Hope the visuals help to understand the concept of variable scoping inside loops.希望视觉效果有助于理解循环内变量作用域的概念。

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

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