Use references/pointers to JS Array declared outside of function

46 Views Asked by At

I am writing a small tictactoe game in Angular.

I have a function checkForWin() which checks all the possible winning combinations for a win. (Horizontal, Vertical, and Diagonal)

The function works correctly when I declare/init potentialWinningCombos inside of checkForWin()

checkForWin(){
  for(let combo of potentialWinningCombos){  
    let potentialWinningCombos: string[][] = [
       //Horizontal
       [this.gb[0][0], this.gb[0][1], this.gb[0][2]],
       [this.gb[1][0], this.gb[1][1], this.gb[1][2]],
...

So that's fine and all, but my original design was to declare/init potentialWinningCombos outside of checkForWin() (in the component -- and of course I'd iterate over this.potentialWinningCombos instead of the local variable)

With this approach I always get empty strings (the initial values).

My original thought was that when I declare/init potentialWinningCombos with a reference to this.gb[0][0], then it would simply be a reference, but it seems that is not the case...

Why? Additionally, is there any possible way to keep my original design (using a reference/pointer) so the object isn't recreated everytime checkForWin() is called? (I know here it's no big deal, but imagine another case where this is a massive object...)

0

There are 0 best solutions below