Object add on - clone

February 14th, 2006

/*
************************************************************
Developed by R.Arul Kumaran [arul@shockwave-india.com]     *
for more code keep visiting [www.shockwave-india.com/blog] *
************************************************************
*/
/*
Object.Clone creates a perfect copy of an object
Modified from FlashGuru’s version to support Arrays
*/
Object.prototype.clone = function() {
if (this instanceof Array) {
var to = [];
for (var i = 0; i< this.length; i++) {
to[i] = (typeof (this[i]) == “object”) ? this[i].clone() : this[i];
}
} else if (this instanceof XML || this instanceof MovieClip) {
// can’t clone this so return null
                var to = null;
trace(“Warning! Object.clone can not be used on MovieClip or XML objects”);
} else {
var to = {};
for (var i in this) {
to[i] = (typeof (this[i]) == “object”) ? this[i].clone() : this[i];
}
}
return to;
};
ASSetPropFlags(Object.prototype, [“clone”], 1);
/*
Usage:-
myObject=new Object()
myObject.name=”Arul”;
myObject.gender=”Male”;
myObject.age=29;
copyObject=myObject.clone();
//now copyObject containes {name:”Arul”, gender:”Male”, age:29};
//changing myObject will not affect copyObject
*/

Entry Filed under: Flash, code

Leave a Comments

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

February 2006
M T W T F S S
« Jan   Mar »
 12345
6789101112
13141516171819
20212223242526
2728  

Most Recent Posts