JavaScript Properties of an object being changed, even though they should be passed by Value

I was working on a small JavaScript project to make a tile based game (not homework, don't worry!), however I ran into a problem with my 'tile' class.

Each 'tile' has an X and a Y property, for its X and Y position on a grid. The grid is stored as a 2 dimensional array of these tiles. The tiles are constructed like so:

class tile {
    constructor(x,y) {
        this.x = x;
        this.y = y;
        console.log(`Genned Tile: ${x} , ${y}`);
    }
}

There's also a small logging message contained

So then, I wrote a small loop to fill an array with 'tile' objects:

for (var x = 0; x < width; x++) {
    for (var y = 0; y < height; y++) {
        tiles[x,y] = new tile(x,y);
    }
}

width and height are both set to 5. This should fill slots 0-4.

However, when displaying the tiles, the X coord is wrong! I'll attach a snippet to show you what I mean

// tile class
class tile {
    constructor(x,y) {
        this.x = x;
        this.y = y;
        console.log(`Genned Tile: ${x} , ${y}`);
    }
}

var width = 5;
var height = 5;
var tiles = new Array(width,height);

// populates array
for (var x = 0; x < width; x++) {
    for (var y = 0; y < height; y++) {
        tiles[x,y] = new tile(x,y);
    }
}

// displays each item in the array
for (var x = 0; x < width; x++) {
    for (var y = 0; y < height; y++) {
        let tile = tiles[x,y];
        console.log(`Checked Tile: ${tile.x} , ${tile.y}`);
    }
}
Answer:1

I'm using Ramda as my functional programming helper library to build a React app. I'm trying to build myself whereAny. Ramda exposes where which checks if every prop it is given satisfies the ...

I'm using Ramda as my functional programming helper library to build a React app. I'm trying to build myself whereAny. Ramda exposes where which checks if every prop it is given satisfies the ...

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...

I have a form on a PHP page : <form name="SendEmail01" method="post"> form items here... </form> I then process it like this: if(isset($_POST["Submit"])) { All tasks here... $...

the problem is simple, document.getElementsByTagName('*') does not select the SVG tag and in the console it gives an error. But if I erase the SVG labels, works correctly. My Code: var Master = ...

the problem is simple, document.getElementsByTagName('*') does not select the SVG tag and in the console it gives an error. But if I erase the SVG labels, works correctly. My Code: var Master = ...

  1. document getelementsbytagname java
  2. document getelementsbytagname innerhtml
  3. document getelementsbytagname w3schools
  4. document getelementsbytagname multiple tags
  5. document getelementsbytagname radio button
  6. document.getelementsbytagname('video') 0 .request picture in picture()
  7. document.getelementsbytagname(name)
  8. document.getelementsbytagname is not a function
  9. document.getelementsbytagname('body')
  10. document.getelementsbytagnamens
  11. document.getelementsbytagname(...).addeventlistener is not a function
  12. document.getelementsbytagname( input )
  13. document.getelementsbytagname foreach
  14. document.getelementsbytagname(...).appendchild is not a function
  15. document.getelementsbytagname( body ) 0
  16. document.getelementsbytagname('head') 0 .appendchild(script)
  17. document.getelementsbytagname jquery
  18. document.getelementsbytagname js
  19. document.getelementsbytagname('base') 0 .href
  20. document.getelementsbytagname('script') 0

There is a function that returns an array according to the second argument (if the country is Japan, it will return only brands of cars from Japan). Is it possible to improve the function using only ...

There is a function that returns an array according to the second argument (if the country is Japan, it will return only brands of cars from Japan). Is it possible to improve the function using only ...

  1. method filter javascript
  2. method filter js
  3. method filter does not exist
  4. method filter java
  5. method filter python
  6. method filter aris
  7. method filter scala
  8. method filter array
  9. method filter swift
  10. method filter in r
  11. filter method feature selection
  12. filter method of ssb generation
  13. filter method in java 8
  14. filter method in typescript
  15. filter method in jquery
  16. filter method in angular 6
  17. filter method in react
  18. filter method in angular
  19. filter method in react js
  20. filter method array javascript