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

后端 未结 11 1849
礼貌的吻别
礼貌的吻别 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:21

    Everything is copied by reference except primitive data types (strings and numbers IIRC).

提交回复
热议问题