Ted Patrick > { Events & Community } > Adobe Systems


copy() for the Object Class

On many projects, I find that adding a simple copy method to the Object Class is very handy. Copy allows you to make a duplicate of any object in the Flash Player that inheirits from Object. It makes data wrangling drop dead simple. ;) \0

Here is some sample usage:

a = [1,2,3,4]
b = a.copy()

c = {a:123, b:'abc'}
d = c.copy()

e = [123, 'abc', [1,2,3], {a:123,b:456}, new XML('<message>Hello Cruel World</message>')]
f = e.copy()

g = _level0.copy()

The method is fairly simply and easy to use. So simple in fact, that it is useful in 100's of situations that are impossible any other way. My favorite use is making components persistant when the primary property data is a complex type (RSDataProvider, Array, XML). It is also useful with Flash Remoting as you can store a local copy of the returned data to enable backtracking without a server roundtrip. In most cases, references cause all sorts of problems where a simple copy can make all the difference.

Notes: copy() stores MovieClips and functions as a reference as these dataTypes cannot be copied effectively. All other datatypes are copied whole. WARNING: Circular references will cause copy() to crash (hence the special treatment of XML & XMLNode). If anyone has a solution for this, please drop me an email, I am all ears. ;)

Download the copy method here.

Cheers,

Ted ;)

3 Responses to “ copy() for the Object Class ”

  1. # Anonymous Karasu

    Thanks, it was just what I needed!


    Cheers,
    Karasu  

  2. # Blogger Chuck

    How do I implement this? I copied it into a .as file, compiled, but no go. I'm using Flex3 Builder.  

  3. # Blogger wezzy

    Hi, Flex has a method to copy objects, ObjectUtil.copy()  

Post a Comment



Jobs


Flex Jobs
city, state, zip


© 2008 Ted On Flash