看完下面的代码,相信对js对象引用又有了新的认识,直接上代码:
// split()把字符串分割成字符串数组
// reverse() 会改变数组本身,**并返回原数组的引用**。!!!!
var arr1 = "john".split(''); // arr1=["j","o","h","n"] var arr2 = arr1.reverse(); // arr1 = arr2 = ["n","h","o","j"]; arr1 and arr2有相同的引用 var arr3 = "jones".split(''); // arr3=["j","o","n","e","s"]; arr2.push(arr3); // arr2=["n","h","o","j",["j","o","n","e","s"]]
// arr1 and arr2有相同的引用 so: arr1=["n","h","o","j",["j","o","n","e","s"]] console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
// array 1: length=5 last=j,o,n,e,s console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));
// array 2: length=5 last=j,o,n,e,s var myArr = [1,2,3,4];
var myArr2 = [5,6,7,8];
console.log(myArr.slice(-1)); // [4]
myArr.push(myArr2);
console.log(myArr); // [1, 2, 3, 4, Array[4]]