11 Replies Latest reply on Aug 9, 2007 1:51 AM by Greg Dove

    Reverse variable assignment using Objects

    cooda
      I've got an Object called "user". I need to duplicate "user" as a new object called "tempUser "and then make changes to "tempUser".

      The problem is that if I say tempUser = user, any changes I make to variables/properties in "tempUser" actually change the variables/properties in "user" as well. Anyone know how to duplicate objects without causing reverse variable assignment?

      cooda
        • 1. Re: Reverse variable assignment using Objects
          Level 7
          cooda,

          > The problem is that if I say tempUser = user, any changes
          > I make to variables/properties in "tempUser" actually change
          > the variables/properties in "user" as well.

          You'll have to create a new Object instance and loop through the
          original to create and set the corresponding properties in your new
          instance. The basic mechanics are laid out here ...

          http://www.quip.net/blog/2006/flash/actionscript-20/duplicating-movie-clips-completely/

          ... that that article discusses MovieClip objects in particular. But the
          principle is the same for Object objects.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Reverse variable assignment using Objects
            Greg Dove Level 4
            That's because variables only refer to complex objects. You need to make a new object and copy all the properties into it.
            If your user object is only one level of hierachy and each property is not complex (i.e. each element is also not an Array or Object or some custom class for example) :

            var tempUser:Object = new Object();

            for (var item in user) { tempUser[item]=user[item]};

            Otherwise you may be better off using a recursive 'clone' type function (try object.clone as2 in google if you're using as2) which traverses the object and copies its properties, creating new instances of arrays or objects and copying them in turn.

            EDIT: (After seeing David's reply) Or save yourself googling it....I'm sure David's example will give you an example of the type of approach you need.
            • 3. Re: Reverse variable assignment using Objects
              cooda Level 1
              Thanks guys. That's pretty much what I had started to do. I just hoped there was an easier way than what I was doing, but no.
              • 4. Re: Reverse variable assignment using Objects
                Greg Dove Level 4
                I was digging around in mx.utils... there's a static class in there that will do it. I'd not noticed it before now.
                e.g.

                import mx.utils.ObjectCopy

                var a = {hello:"hello",b:{c:"c"}}

                d= ObjectCopy.copy(a);

                trace (d.hello)
                trace(d.b.c);

                • 5. Re: Reverse variable assignment using Objects
                  Level 7
                  GWD,

                  > I was digging around in mx.utils... there's a static class
                  > in there that will do it. I'd not noticed it before now.

                  Wow! That's very cool. I had no idea either.


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."


                  • 6. Re: Reverse variable assignment using Objects
                    Greg Dove Level 4
                    @David : Yeah, I might see if I can figure out what some of the others in there are for - I had a look inside a couple of them. Some are clear and easy to understand, some (at least for me anyway) are not. My first guess is that they are intended for databinding and/or webservice support but perhaps they could be used for other things too (they are in 'utils' after all).


                    • 7. Re: Reverse variable assignment using Objects
                      cooda Level 1
                      I did run into one problem using the ObjectCopy class. You can only make a copy from an original. You can't copy a copy. At least as far as I can tell. Anyone else find this to be the case?

                      Cooda
                      • 8. Re: Reverse variable assignment using Objects
                        Level 7
                        Cooda,

                        > I did run into one problem using the ObjectCopy class.
                        > You can only make a copy from an original. You can't
                        > copy a copy. At least as far as I can tell.

                        Sure enough.

                        > Anyone else find this to be the case?

                        Yes. And that's really odd, I think.

                        I found a bit more information in general about this particular class.
                        This is from Darron Schall's blog. (Darron is one of the great ones.)

                        http://www.darronschall.com/weblog/archives/000148.cfm


                        David Stiller
                        Co-author, Foundation Flash CS3 for Designers
                        http://tinyurl.com/2k29mj
                        "Luck is the residue of good design."


                        • 9. Re: Reverse variable assignment using Objects
                          Greg Dove Level 4
                          My experience was similar and I can't be sure why. I guess I just assumed that because its 'official' it works - oops.
                          I suspect it has something to do with the way the constructor function is determined from the original. I tried it with an array of strings and numbers and it works OK twice. But with an object it doesn't.

                          Looking at the code I also can't see how it would, for example, copy a lower level array of reference objects correctly, because it just copies the original array into a new array without considering whether the elements are complex types or not...and electing a recursive call to copy if required but I'm probably missing something and that's unrelated to the problems with a second copy of an object. My brain doesn't work recursively so I always get tangled in knots trying to work out recursive code.

                          Darron Schall's article was good.. he seems to advocate using this class over trying to 'recreate the wheel' but makes no mention of any problems - and it was interesting to see that this class was/is part of the remoting support classes.
                          • 10. Re: Reverse variable assignment using Objects
                            Greg Dove Level 4
                            Ahhh. Spoke too soon. How often do I neglect to read the comments to an article and find the answer there (adobe livedocs has this 'feature' too sometimes!)

                            The answer is in the comments on Darron Schall's page...

                            quote:


                            Hmm.. I think I remember a long time ago running into the same issue, where the copy would return undefined. The answer was a small mistake in the source code.

                            Open up ObjectCopy.as in the mx\util directory of you Flash Installation Classes directory (something like C:\Program Files\Macromedia\Flash MX 2004\en\First Run\Classes), and change this line:

                            var result:Object = new Function( refObj.__proto__.constructor)();


                            .. to be this:

                            var result:Object = new (Function( refObj.__proto__.constructor))();

                            • 11. Reverse variable assignment using Objects
                              Greg Dove Level 4
                              And here's my tweak to it