A couple of big caveats here.

1. mx.utils.ObjectCopy is broken.  (At least, in the Flex 1.5 
distribution.)  It does not correctly preserve the class of an object 
when that class is a subclass of some superclass.  Instead, the copied 
object will appear to have the type of the superclass.

I note that ObjectCopy appears to only be used in net debugging and 
nowhere else.  So this bug isn't exposed via any documented Flex APIs.

Moral: beware of undocumented internal Flash classes, however convenient 
they might look.

2. Even if ObjectCopy worked, you cannot conveniently meld Manish's 
cached-array approach with it, because ObjectCopy will do the wrong 
thing with cycles (it locks up) and multiply referenced objects (it 
duplicates instances).

So.... what to do?

As empirically determined through testing, one correct approach in Flex 
1.5 to instantiating an object that preserves the type of an existing 
one is as follows:

         var newObj;
         if (typeof(obj.__constructor__) == "function")
         {
             // for AS2 classes
             newObj = new obj.__constructor__();
         }
         else if (typeof(obj.constructor) == "function")
         {
             // for Arrays and Objects
             newObj = new obj.constructor();
         }
         else
         {
             // for goodness sake, instantiate *something*!
             newObj = new Object();
         }

I've exhaustively tested this and it works correctly for AS2 classes, 
for Arrays, and for untyped Objects.  You can merge it with Manish's 
code (which I haven't tested, but looks good) to produce a working, 
accurate deep copy.

... .  .    .       .            j


Abdul Qabiz wrote:
> http://manish.revise.org/2005/04/deepcopying-actionscript-objects.html
> 
> http://www.darronschall.com/weblog/archives/000148.cfm 
> 
> 
> -abdul
> 
> 
> -----Original Message-----
> From: [email protected] [mailto:[EMAIL PROTECTED] On
> Behalf Of Pradeep Chaudhary
> Sent: Wednesday, June 22, 2005 7:33 PM
> To: [email protected]
> Subject: [flexcoders] How to create copy of a object
> 
> I want to create a copy for my custom object before it is modified so
> that i can rollback my changes on any error condition. Do we have any
> inbuilt methods or any alternative solution for creating a copy of
> object so that changes made to original object is not reflected copied
> object.
> 
> Pradeep
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
>  
> Yahoo! Groups Links
> 
> 
> 
>  
> 
> 
> 
> 
> 


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/flexcoders/

<*> To unsubscribe from this group, send an email to:
    [EMAIL PROTECTED]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply via email to