class Game { constructor(parentElement) { let wrap = document.createElement('div'); wrap.className = 'wrap'; parentElement.appendChild(wrap); this.nav = document.createElement('div'); this.nav.className = 'nav'; wrap.appendChild(this.nav); this.x = document.createElement('div'); this.x.className = 'x'; this.x.innerHTML = 'win x:' this.nav.appendChild(this.x); this.btn = document.createElement('button'); this.btn.className = 'reset'; this.btn.innerHTML = 'reset'; this.nav.appendChild(this.btn); this.o = document.createElement('div'); this.o.className = 'o'; this.o.innerHTML ='win o:' this.nav.appendChild(this.o); this.field = document.createElement('div'); this.field.className = 'field'; wrap.appendChild(this.field); // this.field = []; for (let i = 0; i < 9; i++) { this.cell = document.createElement('div'); this.cell.className = 'cell'; this.field.appendChild(this.cell); } console.log(this.field.length) } } 

In the Game class, I create a html framework for a mini game. Question: I could create all "variables" (this.x, this.nav, this.btn) through let - as the first (wrap). What is the significant difference ??? do not judge if it is just banal) I just study js and have not yet grown a beard)

    1 answer 1

    Here wrap is a local variable in the constructor:

      let wrap = document.createElement('div'); 

    Here wrap is a property of the object being created:

      this.wrap = document.createElement('div'); 

    If the link is needed only inside the function, use a local variable. If an object reference is needed in other methods or in external code, use an object property.

    Assigning this.cell = ... in a loop is meaningless.

    • _______thank! - Ruslan SSS