Board = function()
{
var cells = [8];
/**
* Initializing every cell using numeric format.
* */
for (var i=0 ; i<8; i++){
cells[i] = [8];
for (var j=0 ; j<8; j++)
cells[i][j] = new Cell(new Position(i,j));
}
....
}
In Another code GameManager.js,
var duplicateBoard = Copy.deepCopy(board);
board.moveCell(1,2)
And for Deepcopying I am using,
Ref : http://jsperf.com/deep-copy-vs-json-stringify-json-parse
function deepCopy(o) {
var copy = o,k;
if (o && typeof o === 'object') {
copy = Object.prototype.toString.call(o) === '[object Array]' ? [] : {};
for (k in o) {
copy[k] = deepCopy(o[k]);
}
}
return copy;
}
My need :
I want cells
(private member of constructor ) in Board
to be deep-copied.
Problem :
But, When I debugged with firebug, I saw, deepCopy
function does not deep copying private objects of constructor.
My Case :
board.moveCell(1,2)
, Here cell[1][2] is moved in duplicateBoard
too.
That is,
No deep-copying of cell
has taken place
Both the board and duplicateBoard has same reference to cell[1][2].
What I have traced ?
The deep-copy function, treats the constructor
to be a function, hence it ignores deep-copying the functions, since it will fail in typeof o === 'object
. But removing this condition is not useful, because by doing so, duplicateBoard
has no functions rather all the functions to be object{}
type.