将新变量设置为JavaScript类实例不会更改原始实例。

huangapple go评论63阅读模式
英文:

Setting a new variable to a javascript class instance doesn't change original instance

问题

我可以为您翻译这段代码中的注释和字符串部分。以下是翻译的内容:

class Grid {
  #cells
  constructor(gameBoardElement) {
    this.#cells = createBoard(gameBoardElement);
  }
  getEmptyCells() {
    return this.#cells.filter((cell) => cell.tile === null)
  }

  getRandomEmptyCell() {
    let emptyCells = this.getEmptyCells();
    if(emptyCells.length > 0) {
      let randomIndex = Math.floor(Math.random() * emptyCells.length);
      return emptyCells[randomIndex];
    }
  }
  handleRight() {
    for(let i = 0; i < 16; i++) {
       if(i % 4 === 3 ) {
        continue;
       }
       this.#combineTwoSquares(i, i+1);
    }
  }

  #combineTwoSquares(firstIndex, secondIndex) {
    let firstCell = this.#cells[firstIndex];
    let secondCell = this.#cells[secondIndex];
    if(firstCell.tile === null) {
      return;
    }
    if(secondCell.tile === null) {

    }
  }
  delete_tiles() {
    for(let i = 0; i < 16; i++) {
      let cell = this.#cells[i];
      if(cell.tile !== null) {
        cell.tile.remove();
        // 这里出现了问题
        cell.tile = null;
        // 这里不会输出null。
        console.log(this.#cells[i].tile)
      }
    }
  }
  get tiles() {
    let tiles = [];
    this.#cells.forEach((cell) => (tiles.push(cell.tile)));
    return tiles;
  }
}

class Cell {
  #cellElement
  #x
  #y
  #tile
  constructor(cellElement,x,y, tile = null) {
    this.#cellElement = cellElement;
    this.#x = x;
    this.#y = y;
    this.#tile = null;
  }

  set tile(tile) {
    if(tile === null) {
      return;
    }
    this.#tile = tile;
    this.#tile.x = this.#x;
    this.#tile.y = this.#y;
  }

  get tile() {
    return this.#tile;
  }
}

function createBoard(gameBoardElement) {
  let cells = []
  for(let i = 0; i < 16; i++) {
    let x = i % 4;
    let y = Math.floor(i/4);
    let cellElement = document.createElement("div");
    cellElement.classList.add("game-cell");
    gameBoardElement.append(cellElement)
    let cell = new Cell(cellElement,x,y);
    cells.push(cell);
  }
  return cells;
}

class Tile {
  #x
  #y
  #value
  #tileElement
  constructor(gameBoard, value = Math.random() > 0.5 ? 2 : 4) {
    this.#tileElement = document.createElement("div");
    this.#tileElement.classList.add("tile");
    gameBoard.appendChild(this.#tileElement);
    this.#tileElement.textContent = value;
    this.#value = value;
  }

  get value() {
    return this.#value;
  }
  set value(value) {
    this.#tileElement.textContent = value
    this.#value = value;
  }

  get x() {
    return this.#x;
  }
  set x(x) {
    this.#x = x;
    this.#tileElement.style.setProperty("--x",this.#x);
  }

  get y() {
    return this.#y;
  }
  set y(y) {
    this.#y = y;
    this.#tileElement.style.setProperty("--y",this.#y);
  }

  remove() {
    this.#tileElement.remove();
  }
}

let gameBoard = document.getElementById("game-board");
window.addEventListener("keydown",handleKeys);
let grid = new Grid(gameBoard);
grid.getRandomEmptyCell().tile = new Tile(gameBoard);

function handleKeys(e) {
    switch(e.key) {
        case "ArrowRight":
            // grid.handleRight();
            grid.delete_tiles();
            // grid.getRandomEmptyCell().tile = new Tile(gameBoard);
            console.log(grid.tiles);
            break;
        default:
        console.log("不是一个有效的按键");
    }
}

如果您需要进一步的翻译或有其他问题,请随时告诉我。

英文:

I'm running into a weird problem. When I set a new variable equal to a class instance, the new variable doesn't mutate the class instance.

Currently, I have a class within my class called #cells that is an array containing cells. Each cell has a property called a tile. When I set a new variable, called tempCell = this.#cells[0] and set tempCell.tile = null, this.#cells[0].tile is not 0. Now when I do this.#cells[i].tile = null, and console log this, I get null. Why is this happening? I've attached code to make clear what I'm doing.

class Grid {
#cells
constructor(gameBoardElement) {
this.#cells = createBoard(gameBoardElement);
}
getEmptyCells() {
return this.#cells.filter((cell) =&gt; cell.tile === null)
}
getRandomEmptyCell() {
let emptyCells = this.getEmptyCells();
if(emptyCells.length &gt; 0) {
let randomIndex = Math.floor(Math.random() * emptyCells.length);
return emptyCells[randomIndex];
}
}
handleRight() {
for(let i = 0; i &lt; 16; i++) {
if(i % 4 === 3 ) {
continue;
}
this.#combineTwoSquares(i, i+1);
}
}
#combineTwoSquares(firstIndex, secondIndex) {
let firstCell = this.#cells[firstIndex];
let secondCell = this.#cells[secondIndex];
if(firstCell.tile === null) {
return;
}
if(secondCell.tile === null) {
}
}
delete_tiles() {
for(let i = 0; i &lt; 16; i++) {
let cell = this.#cells[i];
if(cell.tile !== null) {
cell.tile.remove();
//This is where the problem arises
cell.tile = null;
//this does not print out null. 
console.log(this.#cells[i].tile)
}
}
}
get tiles() {
let tiles = [];
this.#cells.forEach((cell) =&gt; (tiles.push(cell.tile)));
return tiles;
}
}
class Cell {
#cellElement
#x
#y
#tile
constructor(cellElement,x,y, tile = null) {
this.#cellElement = cellElement;
this.#x = x;
this.#y = y;
this.#tile = null;
}
set tile(tile) {
if(tile === null) {
return;
}
this.#tile = tile;
this.#tile.x = this.#x;
this.#tile.y = this.#y;
}
get tile() {
return this.#tile;
}
}
function createBoard(gameBoardElement) {
let cells = []
for(let i = 0; i &lt; 16; i++) {
let x = i % 4;
let y = Math.floor(i/4);
let cellElement = document.createElement(&quot;div&quot;);
cellElement.classList.add(&quot;game-cell&quot;);
gameBoardElement.append(cellElement)
let cell = new Cell(cellElement,x,y);
cells.push(cell);
}
return cells;
}
class Tile {
#x
#y
#value
#tileElement
constructor(gameBoard, value = Math.random() &gt; 0.5 ? 2 : 4) {
this.#tileElement = document.createElement(&quot;div&quot;);
this.#tileElement.classList.add(&quot;tile&quot;);
gameBoard.appendChild(this.#tileElement);
this.#tileElement.textContent = value;
this.#value = value;
}
get value() {
return this.#value;
}
set value(value) {
this.#tileElement.textContent = value
this.#value = value;
}
get x() {
return this.#x;
}
set x(x) {
this.#x = x;
this.#tileElement.style.setProperty(&quot;--x&quot;,this.#x);
}
get y() {
return this.#y;
}
set y(y) {
this.#y = y;
this.#tileElement.style.setProperty(&quot;--y&quot;,this.#y);
}
remove() {
this.#tileElement.remove();
}
}
let gameBoard = document.getElementById(&quot;game-board&quot;);
window.addEventListener(&quot;keydown&quot;,handleKeys);
let grid = new Grid(gameBoard);
grid.getRandomEmptyCell().tile = new Tile(gameBoard);
function handleKeys(e) {
switch(e.key) {
case &quot;ArrowRight&quot;:
// grid.handleRight();
grid.delete_tiles();
// grid.getRandomEmptyCell().tile = new Tile(gameBoard);
console.log(grid.tiles);
break;
default:
console.log(&quot;Not an accurate key&quot;);
}
}

答案1

得分: 1

你在Celltile属性的setter中明确在新值为null时返回而不执行任何操作。你至少应该将this.#tile设置为新值。

set tile(tile) {
	this.#tile = tile;
	if (tile) {
		this.#tile.x = this.#x;
		this.#tile.y = this.#y;
	}
}
英文:

You're explicitly returning without doing anything in the setter for the tile property in Cell when the new value is null. You should at least set this.#tile to the new value.

set tile(tile) {
	this.#tile = tile;
	if (tile) {
		this.#tile.x = this.#x;
		this.#tile.y = this.#y;
	}
}

huangapple
  • 本文由 发表于 2023年6月15日 08:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定