C
Christopher Benson-Manica
Is there a general mechanism to duplicate, or provide for the
duplication of, objects? As an example, suppose I need to duplicate
an array. I can accomplish this with array.slice( 0 ), but that's not
going to cut in general. I could also add a copy method to the Array
prototype, like so:
Array.prototype.copy=function() {
var newArray=[];
for( var idx=0; idx < this.length; idx++ ) {
newArray.push( this[idx] );
}
return newArray;
}
and I could write similar copy() methods for various objects. But I
feel certain that I'm not the first programmer who has had a need for
such functionality. What is the canonical way of doing this?
duplication of, objects? As an example, suppose I need to duplicate
an array. I can accomplish this with array.slice( 0 ), but that's not
going to cut in general. I could also add a copy method to the Array
prototype, like so:
Array.prototype.copy=function() {
var newArray=[];
for( var idx=0; idx < this.length; idx++ ) {
newArray.push( this[idx] );
}
return newArray;
}
and I could write similar copy() methods for various objects. But I
feel certain that I'm not the first programmer who has had a need for
such functionality. What is the canonical way of doing this?