bnthsrikanth
bnthsrikanth

Reputation: 9

Accessing a module array gives me a reference error

I am trying to understand the revealing-module pattern but when try to update or access the variables falling into errors and can anyone provide an example how to update/access the variables in revealing-module pattern

I get

main.js:20 Uncaught ReferenceError: board is not defined at main.js:20

var module = (function() {

  var publicVariable = "X";
  var board = new Array(9).fill(null)

  return {
    //This function can access `publicVariable` !
    changePlayer: function() {
      return (publicVariable = publicVariable === "X" ? "O" : "X")
    },
    MakeMove: function(i) {
      return (board[i] = publicVariable)
    },
    board: board

  }

})();
module.MakeMove(2)
console.log(board[2])

Upvotes: -1

Views: 57

Answers (1)

Kyruski
Kyruski

Reputation: 259

You are returning an object, so in order to access board, you'd have to do console.log(module.board[2]).

Upvotes: 1

Related Questions