我試圖複製一個數組的時候,但是我不斷收到問題。我嘗試過兩種不同的方式,但都沒有成功。錯誤導致試圖複製一個數組
第一次嘗試:
function classA(id, arrayFrom, arrayTo)
{
this.id = id;
this.from = arrayFrom.slice(0);
this.to = arrayTo.slice(0);
};
輸出:
Uncaught TypeError: arrayFrom.slice is not a function
第二次嘗試:
function classA(id, arrayFrom, arrayTo)
{
this.id = id;
this.from = {arrayFrom[0], arrayFrom[1], arrayFrom[2]};
this.to = {arrayTo[0], arrayTo[1], arrayTo[2]};
};
輸出:
Uncaught SyntaxError: Unexpected token [
無用的細節。與該函數的調用共享代碼。 –
這些方法很好,無論你傳給它們,它們都不是數組 – Yoda
'arrayFrom'是* not *數組。請告訴我們它的實際情況。 –