2016-02-28 115 views
2


我試圖推動新的數組的一個數組的位置:推新陣列爲二維陣列

var history = [ ]; 

function addHistory(array) 
{ 
history.push(array); 
console.log(history)//to check what is the value of array 
} 

var test1 = [0,0,0,0,0]; 
var test2 = [1,1,1,1,1]; 
addHistory(test1); 
addHistory(test2); 

做這樣後,數組應該是:

[ [0,0,0,0,0] , [1,1,1,1,1] ] 

而是將其打印

[ [1,1,1,1,1] , [1,1,1,1,1] ] 

所以基本上它取代所有舊的陣列,陣列「歷史」,inste推廣到最後的廣告。

這裏有什麼問題?
非常感謝

編輯:
對不起,忘了提,我的實際變量不叫歷史(我把它叫做這樣只是你能想象我想要的)。它被稱爲「rollHist

+0

對不起,我的錯,它與每一個名字相同..也許有插入新陣列的另一種方式成數組? – Nedas

回答

2

history是javascript中的一個受保護術語。它更改爲這將解決它:

var myHistory = []; 

function addHistory(array) 
{ 
myHistory.push(array); 
console.log(myHistory)//to check what is the value of array 
} 

var test1 = [0,0,0,0,0]; 
var test2 = [1,1,1,1,1]; 
addHistory(test1); 
addHistory(test2); 

你可以閱讀更多有關各種保護字here

+3

這不是一個錯誤。 「歷史」確實是一個保留字,就是這樣。 –

+0

不是一個bug,歷史是一個全局對象 – Ramanlfc

+0

對不起,我的意思是說,作爲他的代碼中的錯誤!我已經刪除了這個詞哈哈 – millerbr