0

i make stack2[0].matrix refer to stack1[0].matrix, but after i change stack2[0].matrix, the stack1[0].matrix is changed too. why is this happen? and how can i keep stack1[0].matrix value?

thank you


let Number = function(){
    this.matrix = [
        [1, 2, 3],
        [4, 5, 6]
    ]
}

stack1 = [];
stack2 = [];

stack1.push(new Number);
stack2.push(new Number);
stack2[0].matrix = stack1[0].matrix;
stack2[0].matrix[0][0] = 5;

console.log(stack1[0].matrix);
console.log(stack2[0].matrix);
Engineer S. Saad
  • 378
  • 4
  • 19

0 Answers0