不知道可否有人試過復(fù)制多維數(shù)組變量后,修改原來的數(shù)組變量,而拷貝的變量值也一起更換呢?
如:
var = new Array([1,2]);
var = myArray;
myArray[0][0] = 3;
trace(myArray); //3,2
trace(temp); //沒更動(dòng)但卻輸出3,2
var = myArray;
myArray[0][0] = 3;
trace(myArray); //3,2
trace(temp); //沒更動(dòng)但卻輸出3,2
而經(jīng)過測試之后,在一維數(shù)組卻沒有問題。所以從此處寫出修正的方法
Array.prototype.duplicate = function(){
var a = [];
for (var i in this) (this[i] instanceof Array) ? a[i] = this[i].slice() : a[i] = this[i];
return a;
}
var = new Array();
myArray[0]=[1,2]
myArray[1]=3
myArray[2]=[5,6]
var = myArray.duplicate();
trace(myArray); //1,2,3,5,6
trace(temp); //1,2,3,5,6
myArray[0][0] = 5;
trace(myArray); //5,2,3,5,6
trace(temp); //1,2,3,5,6
var a = [];
for (var i in this) (this[i] instanceof Array) ? a[i] = this[i].slice() : a[i] = this[i];
return a;
}
var = new Array();
myArray[0]=[1,2]
myArray[1]=3
myArray[2]=[5,6]
var = myArray.duplicate();
trace(myArray); //1,2,3,5,6
trace(temp); //1,2,3,5,6
myArray[0][0] = 5;
trace(myArray); //5,2,3,5,6
trace(temp); //1,2,3,5,6