Re: [Flashcoders] Duplicating an object without pointing to original one

2006-01-10 Thread Rajat Paharia
I use this function (think I got it from Darron Schall's blog) to do a deep
copy. I had the same experience as Jesse with mx.utils.ObjectCopy not
working correctly...

// --
// copy an object
function copyObject(obj) {
 // create a "new" object or array depending on the type of obj
 var copy = (obj instanceof Array) ? [] : {};

 // loop over all of the value in the object or the array to copy them
 for(var i in obj) {
 // assign a temporarity value for the data inside the object
 var item = obj[i];
  // check to see if the data is complex or primitive
 switch(item instanceof Array || item instanceof Object) {
 case true:
// if the data inside of the complex type is still complex,
we need to
   // break that down further, so call copyObject again on that
complex
   // item
   copy[i] = copyObject(item);
break;
default:
   // the data inside is primitive, so just copy it (this is a
value copy)
   copy[i] = item;
 }
 }
   return copy;
}

best, - rajat

On 1/10/06, zwetan <[EMAIL PROTECTED]> wrote:
>
>
> Hi,
>
> >
> > How do you duplicate an object {bool:true, val:5} in another variable
> > without pointing to the original one, and without having to create a
> > constructor.  In Director, there's a duplicate() method in Lingo.  Is
> > there an equivalent way to do this in Flash?
> >
> > Something like this, but which would actually work:
> >
> > var h = {a:true};
> > var g = h;
> > trace(h.a); // true
> > g.a = false;
> > trace(h.a); // false, but want to be true, so it's separate from g
> > object.
> >
>
> I recently released a library which add this functionality
> of deep copy to all core objects
>
> You can download it here
> http://www.burrrn.com/projects/core2.html
>
>
> your small exemple will turn like that:
>
> var h = { a:true };
> var g = h.copy();
> trace( h.a ); //true
> g.a = false;
> trace( h.a ); //true
>
>
>
> zwetan
>
>
>
>
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>



--
Rajat Paharia
[EMAIL PROTECTED]
http://www.bunchball.com
http://www.rootburn.com
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


RE: [Flashcoders] Duplicating an object without pointing to original one

2006-01-10 Thread zwetan

Hi,

>
> How do you duplicate an object {bool:true, val:5} in another variable
> without pointing to the original one, and without having to create a
> constructor.  In Director, there's a duplicate() method in Lingo.  Is
> there an equivalent way to do this in Flash?
>
> Something like this, but which would actually work:
>
> var h = {a:true};
> var g = h;
> trace(h.a); // true
> g.a = false;
> trace(h.a); // false, but want to be true, so it's separate from g
> object.
>

I recently released a library which add this functionality
of deep copy to all core objects

You can download it here
http://www.burrrn.com/projects/core2.html


your small exemple will turn like that:

var h = { a:true };
var g = h.copy();
trace( h.a ); //true
g.a = false;
trace( h.a ); //true



zwetan





___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Duplicating an object without pointing to original one

2006-01-09 Thread Mark Walters
If you download the FlashRemoting source code, (
http://download.macromedia.com/pub/flashremoting/mx2004/components/actionscript_2.0/flashremoting_comp_sourcecode.zip
), you will have a new class mx.utils.ObjectCopy ... this will make a
new object, instead of a reference to the first.


On 1/9/06, JesterXL <[EMAIL PROTECTED]> wrote:
> For shallow copies, you can use a for in loop.
>
> var o:Object = {label: "test", data: 5};
> var copy:Object = {};
> for(var p in o)
> {
> copy[p] = o[p];
> }
>
> However, if the o has properties that are objects, or arrays, you'll have
> problems because your copy will still contain references.  You need to
> utilize a recursive algorithm to continually dig down on the object.
> Recursion in Flash, however, is dangerous since you have a limit of 256, and
> adjusting this in Flex only further increases the danger of locking up the
> comp.
>
> My suggestion is to provide a clone (or duplicate) method to those data
> objects you use to copy, and leave the implementation details to that object
> rather than creating a catch-all method.
>
> Example:
>
> class MyObject
> {
> public var label:String;
> public var data:Number;
>
> public function clone():MyObject
> {
> var o:MyObject = new MyObject();
> o.label = label;
> o.data = data;
> return o;
> }
> }
>
> - Original Message -
> From: "Mendelsohn, Michael" <[EMAIL PROTECTED]>
> To: "Flashcoders mailing list" 
> Sent: Monday, January 09, 2006 12:05 PM
> Subject: [Flashcoders] Duplicating an object without pointing to original
> one
>
>
> Hi list...
>
> How do you duplicate an object {bool:true, val:5} in another variable
> without pointing to the original one, and without having to create a
> constructor.  In Director, there's a duplicate() method in Lingo.  Is
> there an equivalent way to do this in Flash?
>
> Something like this, but which would actually work:
>
> var h = {a:true};
> var g = h;
> trace(h.a); // true
> g.a = false;
> trace(h.a); // false, but want to be true, so it's separate from g
> object.
>
> - MM
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> ___
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


Re: [Flashcoders] Duplicating an object without pointing to original one

2006-01-09 Thread JesterXL
For shallow copies, you can use a for in loop.

var o:Object = {label: "test", data: 5};
var copy:Object = {};
for(var p in o)
{
copy[p] = o[p];
}

However, if the o has properties that are objects, or arrays, you'll have 
problems because your copy will still contain references.  You need to 
utilize a recursive algorithm to continually dig down on the object. 
Recursion in Flash, however, is dangerous since you have a limit of 256, and 
adjusting this in Flex only further increases the danger of locking up the 
comp.

My suggestion is to provide a clone (or duplicate) method to those data 
objects you use to copy, and leave the implementation details to that object 
rather than creating a catch-all method.

Example:

class MyObject
{
public var label:String;
public var data:Number;

public function clone():MyObject
{
var o:MyObject = new MyObject();
o.label = label;
o.data = data;
return o;
}
}

- Original Message - 
From: "Mendelsohn, Michael" <[EMAIL PROTECTED]>
To: "Flashcoders mailing list" 
Sent: Monday, January 09, 2006 12:05 PM
Subject: [Flashcoders] Duplicating an object without pointing to original 
one


Hi list...

How do you duplicate an object {bool:true, val:5} in another variable
without pointing to the original one, and without having to create a
constructor.  In Director, there's a duplicate() method in Lingo.  Is
there an equivalent way to do this in Flash?

Something like this, but which would actually work:

var h = {a:true};
var g = h;
trace(h.a); // true
g.a = false;
trace(h.a); // false, but want to be true, so it's separate from g
object.

- MM

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders 

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders


[Flashcoders] Duplicating an object without pointing to original one

2006-01-09 Thread Mendelsohn, Michael
Hi list...

How do you duplicate an object {bool:true, val:5} in another variable
without pointing to the original one, and without having to create a
constructor.  In Director, there's a duplicate() method in Lingo.  Is
there an equivalent way to do this in Flash?

Something like this, but which would actually work:

var h = {a:true};
var g = h;
trace(h.a); // true
g.a = false;
trace(h.a); // false, but want to be true, so it's separate from g
object.

- MM

___
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders