|
I'm sure there are better ways to do this, but
Array.prototype.copy = function() { setTimeout("var r = " + this.toSource(), 1); return r; }
|
|
|
|
One way I've seen to do it but could never get to work is either this.splice(0,0) or this.slice(0,0) (I always get the two confused.) I've never managed to get it to work as said though.
|
|
|
|
|
I decided to play around a bit, and came up with this, var e = ["hello", "world"] var r = e.concat(); Rediculously simple, but that's what bothers me. It's too simple >_>
Last Edit: Jul 18, 2007 5:12:37 GMT by Aaron
|
|
|
|
So wait, would this work then?
Array.prototype.copy = function(){ return this.concat(); }
o.O Weird. Just make sure that you test that changing one array doesn't effect the other.
|
|
|
|
|
I did.
var e = ["hello", "world"] var r = e.concat(); e.splice(0, 1); alert(e) alert(r)
|
|
|
|
Array.prototype.copy = function(){ return this.slice(0); }
Slice takes two parameters, starting index and length, I believe. Specifying no length goes until it reaches the end of the array. e = ["r","t"]; r = e.slice(0); e[0] = "t"; alert(e[0]+r[0]); // Alerts tr
|
#intj (Mastermind)^
|
|
|