Yesterday I was encounter a problem of storing a Object in another Object and making changes to that Object in ActionScript 1.0. Now what Flash do, it will create the reference of that Object and will not create a copy of that Object so if you make any changes to any of the property of first object it will also update second object automatically. Now to overcome this problem I have created a small piece of code to make the copy of Object rather than storing the reference of the Object. I hope it will help you too:-
Code:
Object.prototype.copy = function ()
{
var prop, obj = new this.__proto__.constructor ();
for (prop in this)
{
if (typeof this[prop] == "object")
{
obj[prop] = this[prop].copy ();
}
else
{
obj[prop] = this[prop];
}
}
return obj;
};
Usage:var myObj1:Object = new Object();
myObj1.prop1 = "myValue";
var myObj2:Object = myObj1.copy();
myObj2.prop1 = "myValue2";
trace(myObj1.prop1);
trace(myObj2.prop1);
0 comments:
Post a Comment