以下是代碼片段。基本上,'this.leaves'是一個數組。我想要移動第一個數組元素,複製它(稱爲frontLeaf),並將其移到原始數組中,從複製的元素中更改一些屬性,然後將該元素放到父數組元素中。如何通過'傳遞值'而不是'傳遞參考'來複制元素
var frontLeaf = this.leaves.shift();
this.leaves.unshift(frontLeaf);
frontLeaf.leftChild = tmp;
frontLeaf.rightChild = this;
this.parent.leaves.push(frontLeaf);
我的問題是frontLeaf似乎通過引用傳遞,當我分配
frontLeaf.leftChild = tmp;
frontLeaf.rightChild = this;
上面兩行代碼似乎影響在this.leaves和this.parent.leaves兩個元件。那麼,我該如何解決這個問題呢?
今天是學習JavaScript我人生的轉折點。 – user482594 2011-04-18 05:04:11