var ProtVar = function(arr){
this.gr1 = [];
this.gr2 = [];
this.arr = arr;
return this;
}
ProtVar.prototype.setArrs = function(){
this.gr1 = this.arr;
this.gr2 = this.arr;
return this;
}
ProtVar.prototype.shiftGr1 = function(){
this.gr1.shift();
return this;
}
ProtVar.prototype.showData = function(){
console.log("gr1 = ", this.gr1.length, this.gr1);
console.log("gr2 = ", this.gr2.length, this.gr2);
console.log("arr = ", this.arr.length, this.arr);
return this;
}
var protVar = new ProtVar([1,2,3,4]).setArrs().shiftGr1().showData();
如何在不链接到同一阵列的情况下复制数组?
我想出了如何使用slice(0);
执行此操作,请参阅下文。
ProtVar.prototype.setArrs = function(){
this.gr1 = this.arr.slice(0);
this.gr2 = this.arr.slice(0);
return this;
}
这是正确的方法吗?