Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
I'm sure there are better ways to do this, but

Array.prototype.copy = function() {
setTimeout("var r = " + this.toSource(), 1);
return r;
}



Chris

Chris Avatar

******
Head Coder

19,519


June 2005
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.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
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

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
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.

Aaron

Aaron Avatar
Bad Wolf

****
Dedicated Studio Member

859


November 2006
I did.

var e = ["hello", "world"]
var r = e.concat();
e.splice(0, 1);
alert(e)
alert(r)

Tobias

Tobias Avatar

***
Dedicated Member

182


November 2006
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)^

newBookmarkLockedFalling