Why does changing an Array in JavaScript affect copies of the array?

后端 未结 11 1835
礼貌的吻别
礼貌的吻别 2020-11-22 00:16

I\'ve written the following JavaScript:

var myArray = [\'a\', \'b\', \'c\'];
var copyOfMyArray = myArray;
copyOfMyArray.splice(0, 1);
alert(myArray); // aler         


        
11条回答
  •  名媛妹妹
    2020-11-22 00:20

    In JS, operator "=" copy the pointer to the memory area of the array. If you want to copy an array into another you have to use the Clone function.

    For integers is different because they are a primitive type.

    S.

提交回复
热议问题