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

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

    You can add some error handling depending on your cases and use something similar to the following function to solve the issue. Please comment for any bugs / issues / efficiency ideas.

    function CopyAnArray (ari1) {
       var mxx4 = [];
       for (var i=0;i

提交回复
热议问题