const module = (function() {
let _priv = {a:1};
return {
get priv() {return _priv}
}
})();
let obj = module.priv;
obj.b = 2;
console.log(module.priv); //{a:1, b:2}
Using a factory function (or revealing module pattern in this case), how do I 'get' a private object for reference but have it be immutable?
A more practical example is for a game of tic-tac-toe:
const gameBoard = (function() {
let _board = (new Array(9)).fill(''); //want it to be immutable from the outside
const add = (index,mark) => {
_board[index] = mark;
}
const getBoard = () => {return _board}
return {add, getBoard}
})();
I want _board to only be changed by the add() method, but I also want a reference to the board's state in other places in the code. But with this current code the board is exposed and can be altered.