简体   繁体   English

为什么我的变量不起作用,但定义起作用了?

[英]Why does my variable not work, but the definition does?

I am running into this problem that I can't understand.我遇到了这个我无法理解的问题。 I am doing an exercise that uses DOM manipulation in order to create a grid in javascript.我正在做一个使用 DOM 操作的练习,以便在 javascript 中创建一个网格。 I defined a variable, and then included that variable inside a loop.我定义了一个变量,然后将该变量包含在一个循环中。 It only created one div, but it printed my console.log 16 times like I asked.它只创建了一个 div,但它像我问的那样打印了我的 console.log 16 次。 So I defined the variable instead directly inside the loop, and that worked all 16 times.所以我直接在循环内部定义了变量,并且所有 16 次都有效。 Can someone help me understand what concept about scope I do not seem to understand?有人可以帮助我理解我似乎不理解的关于范围的概念吗?

This works:这有效:

let gridHome = document.getElementById('grid');
let grid = function() {
    for (i = 0; i < 16; i++){
        console.log('added a div');
        let gridBlock = document.createElement('div');
        gridBlock.setAttribute('class', 'block');
        gridHome.appendChild(gridBlock);
        console.log('run it again');
    }
}
grid();

This creates only one div but runs all 16 console.logs:这仅创建一个 div,但运行所有 16 个 console.logs:

let gridHome = document.getElementById('grid');
let grid = function() {
    let gridBlock = document.createElement('div')
    for (i = 0; i < 16; i++){
        console.log('added a div');
        gridBlock;
        gridBlock.setAttribute('class', 'block');
        gridHome.appendChild(gridBlock);
        console.log('run it again');
    }
}
grid();

Any help is greatly appreciated, thank you!非常感谢任何帮助,谢谢!

@hashbrowns94 It's not just scope thing, It's Javascript. @hashbrowns94 这不仅仅是范围的事情,它是 Javascript。 I'll go ahead and break it down for you.我会继续为你分解它。 The first works this way;第一个是这样工作的; You define gridHome .您定义gridHome Which will holds all the div fragments created inside the loop.它将保存在循环内创建的所有 div 片段。 So every time the loop ends you stack up gridHome with the new div created INSIDE the loop and that way it goes on till the loop ends.因此,每次循环结束时,您都会将gridHome与在循环内部创建的新 div 堆叠起来,这样它就会一直持续到循环结束。

In the other solution, you define GridHome to holds the fragments too.在另一个解决方案中,您也定义了GridHome来保存片段。 But notice the GridBlock is define outside the loop and it's also define inside the loop too but this time set to undefined (NB reference error).但是请注意GridBlock是在循环外定义的,它也在循环内定义,但这次设置为未定义(NB 引用错误)。 The one inside the loop take precedence..and it's simply undefined (usually this should give you a reference error).循环内的那个优先......而且它只是未定义的(通常这应该给你一个参考错误)。 So in the end, only one div is appended to the GridHome which is the one define OUTSIDE the loop.因此,在结束时,只有一个DIV附加到这是一个限定OUTSIDE循环中的GridHome。

Just to mention use const instead of let as shown below.只是提到使用const而不是let ,如下所示。

 const gridHome = document.getElementById('grid'); let grid = function() { for (i = 0; i < 16; i++){ console.log('added a div'); const gridBlock = document.createElement('div'); gridBlock.setAttribute('class', 'block'); gridHome.appendChild(gridBlock); console.log('run it again'); } } grid();
 .block { height: 10px; width: 100px; background: blue; margin: 5px; }
 <div id="grid"> </div>

PART TWO Try and play with this to understand what really going on..第二部分尝试玩这个以了解真正发生的事情..

 let gridHome = document.getElementById('grid'); let grid = function() { let gridBlock = document.createElement('div') for (i = 0; i < 16; i++){ console.log('added a div'); //gridBlock; gridBlock.setAttribute('class', 'block'); gridHome.appendChild(gridBlock); console.log('run it again'); } } grid();
 .block { height: 10px; width: 100px; background: blue; margin: 5px; }
 <div id="grid"> </div>

Regards.问候。

let gridHome = document.getElementById('grid');
let grid = function() {
    //let gridBlock = document.createElement('div')
    for (i = 0; i < 16; i++){
        let gridBlock = document.createElement('div') // Use this line here 
        console.log('added a div');
        gridBlock.setAttribute('class', 'block');
        gridHome.appendChild(gridBlock);
        console.log('run it again');
    }
}
grid();

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

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