javascript引用奇趣

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script>

var arr1 = [1,2,3];
var arr2 = arr1;//这个赋值的过程其实只是地址指向
arr2.push(4);

alert(arr1);//[1,2,3,4]
alert(arr2);//[1,2,3,4]

   //要解决上述问题,直接赋值
    var arr1 = [1,2,3];
    var arr2 = [];

    for(var i=0;i<arr1.length;i++){
        arr2.push(arr1[i]);
     //arr2[i] = arr1[i];//性能更优 }
</script> </body> </html>

 

posted @ 2016-03-29 17:21  雨落知音  阅读(117)  评论(0编辑  收藏  举报