Object add on - clone
/* ************************************************************ 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 */
Add comment February 14th, 2006