Javascript范围 - 需要保留原始值

时间:2015-06-13 15:22:19

标签: javascript scope

var oldArray = [],someArray=[],newArray=[];

oldArray = [4,5];
someArray =[1,2,3]; 
newArray = someArray;

for (var i=0; i<oldArray.length; i++){
    someArray.push(oldArray[i]);
}

// I want this to be [1,2,3] but it becomes [1,2,3,4,5]
// due to scope issue I believe
oldArray = newArray; 
newArray时,{p> someArray必须是[1,2,3]的原始oldArray = newArray,但由于oldArray = newArray [1,2,3,4,5]成为someArray代替。

我该如何解决这个问题?我需要保留原始oldArray值并将这些值分配到addEventListener

0 个答案:

没有答案