I have the following struct (or class?) in JavaScript:
function ImageStruct() {
this.id = -1;
this.isCover = true;
this.isPaired = false;
this.row = -1;
this.column = -1;
}
I add this class to a bidimensional array.
var imgStruct = new ImageStruct();
imgStruct.id = id;
imgStruct.row = row;
imgStruct.column = column;
$.myNameSpace.matrixPos[row][column] = imgStruct;
When I do something like this:
var imgStrc = $.myNameSpace.matrixPos[row][column];
If I modify imgStrc, object in $.myNameSpace.matrixPos[row][column] doesn’t reflect that changes.
Is there any way to ‘fix’ this?
If you modify
imgStrcit by changing its properties (e.g. by doingimgStrc.id = 42), that change will affect the object in$.myNameSpace.matrixPos[row][column](as it is in fact the same object).Only if you modify
imgStrcby reassigning it, it won’t. There is no way to ‘fix’ this, other than setting$.myNameSpace.matrixPos[row][column] = imgStrcor wrapping the whole thing into a wrapper object (or one-element array).