<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ADM Blog &#187; clone</title>
	<atom:link href="http://blog.another-d-mention.ro/tag/clone/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.another-d-mention.ro</link>
	<description>No matter how you see things, reality changes when you reach understanding</description>
	<lastBuildDate>Wed, 11 Aug 2010 08:07:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>How to clone (duplicate) an object in ActionScript 3</title>
		<link>http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/</link>
		<comments>http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/#comments</comments>
		<pubDate>Fri, 05 Jun 2009 20:33:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Flex / ActionScript]]></category>
		<category><![CDATA[How To's]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[actionscript]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[clone]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[flex]]></category>

		<guid isPermaLink="false">http://blog.another-d-mention.ro/?p=440</guid>
		<description><![CDATA[For a project I needed to clone an object of unknown type. And by clone I mean to create a new instance of that same type and then fill out all its properties (including getters and setters) to mirror the original object. Thanks to a friend, I discovered the describeType function in AS3. But this alone [...]


Related posts:<ol><li><a href='http://blog.another-d-mention.ro/programming/flex-actionscript/serialize-javascript-object-to-json/' rel='bookmark' title='Permanent Link: Serialize JavaScript object to JSON'>Serialize JavaScript object to JSON</a></li>
<li><a href='http://blog.another-d-mention.ro/programming/neural-networks-in-actionscript-3/' rel='bookmark' title='Permanent Link: Neural networks in ActionScript 3'>Neural networks in ActionScript 3</a></li>
<li><a href='http://blog.another-d-mention.ro/programming/how-to-identify-at-runtime-if-swf-is-in-debug-or-release-mode-build/' rel='bookmark' title='Permanent Link: How to identify at runtime if the swf is in debug or release mode/build'>How to identify at runtime if the swf is in debug or release mode/build</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>For a project I needed to clone an object of unknown type. And by clone I mean to create a new instance of that same type and then fill out all its properties (including getters and setters) to mirror the original object.</p>
<p>Thanks to a friend, I discovered the <a href="http://livedocs.adobe.com/flex/2/langref/flash/utils/package.html#describeType%28%29">describeType </a>function in AS3. But this alone will only take care of the copying part. To create an object of the same type as another one we use <a href="http://livedocs.adobe.com/flex/2/langref/flash/utils/package.html#getDefinitionByName%28%29">getDefinitionByName</a>.</p>
<p>Although Flash reflection is pretty basic, with a little work it will do the trick.</p>
<p>Get the <a href="http://blog.another-d-mention.ro/stuff/cloneTest.zip">application files</a>.</p>
<p>Here's the code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;</span>mx <span style="color: #000000; font-weight: bold;">:</span>Application xmlns<span style="color: #000000; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #990000;">&quot;absolute&quot;</span> xmlns<span style="color: #000000; font-weight: bold;">:</span>local=<span style="color: #990000;">&quot;*&quot;</span> creationComplete=<span style="color: #990000;">&quot;init()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
<span style="color: #000000; font-weight: bold;">&lt;/</span>mx<span style="color: #000000; font-weight: bold;">&gt;&lt;</span>mx <span style="color: #000000; font-weight: bold;">:</span>Script<span style="color: #000000; font-weight: bold;">&gt;</span>
 <span style="color: #000000; font-weight: bold;">&lt;</span> <span style="color: #000000; font-weight: bold;">!</span><span style="color: #000000;">&#91;</span>CDATA<span style="color: #000000;">&#91;</span>
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">import</span> mx.controls.Alert;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">source</span><span style="color: #000000; font-weight: bold;">:</span>DataObject = <span style="color: #0033ff; font-weight: bold;">new</span> DataObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
     <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> cloneObject<span style="color: #000000; font-weight: bold;">:</span>DataObject;
&nbsp;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">init</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
         <span style="color: #004993;">source</span>.<span style="color: #004993;">name</span> = <span style="color: #990000;">'John Doe'</span>;
         <span style="color: #004993;">source</span>.howMany = <span style="color: #000000; font-weight:bold;">4.5</span>;
         <span style="color: #004993;">source</span>.when = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Date</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>;
         <span style="color: #004993;">source</span>.complexProp = <span style="color: #0033ff; font-weight: bold;">new</span> DataObject<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
         <span style="color: #004993;">source</span>.complexProp.<span style="color: #004993;">name</span> = <span style="color: #990000;">'Name in sub-object'</span>;
&nbsp;
         cloneObject = UtilFunctions.<span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> DataObject;
&nbsp;
         Alert.<span style="color: #004993;">show</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Clone:<span style="">\n</span>name = &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> cloneObject.<span style="color: #004993;">name</span> <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>howMany = &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> cloneObject.howMany <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>when = &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> cloneObject.when <span style="color: #000000; font-weight: bold;">+</span> <span style="color: #990000;">&quot;<span style="">\n</span>complexProp.name = &quot;</span> <span style="color: #000000; font-weight: bold;">+</span> cloneObject.complexProp.<span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span>;
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #3f5fbf;">/**
&nbsp;
      * describeType will produce this (for a DataObject instance):
      *
      * &lt;type name=&quot;DataObject&quot; base=&quot;Object&quot; isDynamic=&quot;false&quot; isFinal=&quot;false&quot; isStatic=&quot;false&quot;&gt;
&nbsp;
           &lt;extendsclass type=&quot;Object&quot;/&gt;
           &lt;accessor name=&quot;isHandicap&quot; access=&quot;writeonly&quot; type=&quot;Boolean&quot; declaredBy=&quot;DataObject&quot;/&gt;
&nbsp;
           &lt;variable name=&quot;howMany&quot; type=&quot;Number&quot;/&gt;
           &lt;accessor name=&quot;complexProp&quot; access=&quot;readwrite&quot; type=&quot;DataObject&quot; declaredBy=&quot;DataObject&quot;/&gt;
&nbsp;
           &lt;variable name=&quot;name&quot; type=&quot;String&quot;/&gt;
           &lt;variable name=&quot;when&quot; type=&quot;Date&quot;/&gt;
&nbsp;
&nbsp;
      *
      * */</span>
&nbsp;
 <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;/</span>mx<span style="color: #000000; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>And the UtilFunctions.as file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
 <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">describeType</span>;
 <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getDefinitionByName</span>;
 <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.utils</span>.<span style="color: #004993;">getQualifiedClassName</span>;
&nbsp;
 <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> UtilFunctions
 <span style="color: #000000;">&#123;</span>
&nbsp;
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> newSibling<span style="color: #000000;">&#40;</span>sourceObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:*</span> <span style="color: #000000;">&#123;</span>
         <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>sourceObj<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
             <span style="color: #6699cc; font-weight: bold;">var</span> objSibling<span style="color: #000000; font-weight: bold;">:*</span>;
             <span style="color: #0033ff; font-weight: bold;">try</span> <span style="color: #000000;">&#123;</span>
                 <span style="color: #6699cc; font-weight: bold;">var</span> classOfSourceObj<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Class</span> = <span style="color: #004993;">getDefinitionByName</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">getQualifiedClassName</span><span style="color: #000000;">&#40;</span>sourceObj<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #0033ff; font-weight: bold;">as</span> <span style="color: #004993;">Class</span>;
                 objSibling = <span style="color: #0033ff; font-weight: bold;">new</span> classOfSourceObj<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;
             <span style="color: #000000;">&#125;</span>
&nbsp;
             <span style="color: #0033ff; font-weight: bold;">catch</span><span style="color: #000000;">&#40;</span>e<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><span style="color: #000000;">&#125;</span>
&nbsp;
             <span style="color: #0033ff; font-weight: bold;">return</span> objSibling;
         <span style="color: #000000;">&#125;</span>
         <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #0033ff; font-weight: bold;">null</span>;
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">clone</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span> <span style="color: #000000;">&#123;</span>
&nbsp;
         <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">clone</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>;
         <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
             <span style="color: #004993;">clone</span> = newSibling<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000;">&#41;</span>;
&nbsp;
             <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">clone</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                 copyData<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span>, <span style="color: #004993;">clone</span><span style="color: #000000;">&#41;</span>;
             <span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#125;</span>
&nbsp;
         <span style="color: #0033ff; font-weight: bold;">return</span> <span style="color: #004993;">clone</span>;
     <span style="color: #000000;">&#125;</span>
&nbsp;
     <span style="color: #0033ff; font-weight: bold;">public</span> static <span style="color: #339966; font-weight: bold;">function</span> copyData<span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span>, destination<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span> <span style="color: #000000;">&#123;</span>
&nbsp;
         <span style="color: #009900;">//copies data from commonly named properties and getter/setter pairs</span>
         <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000;">&#41;</span> <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #000000;">&#40;</span>destination<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
             <span style="color: #0033ff; font-weight: bold;">try</span> <span style="color: #000000;">&#123;</span>
                 <span style="color: #6699cc; font-weight: bold;">var</span> sourceInfo<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span> = <span style="color: #004993;">describeType</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">source</span><span style="color: #000000;">&#41;</span>;
                 <span style="color: #6699cc; font-weight: bold;">var</span> prop<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">XML</span>;
&nbsp;
                 <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span>prop <span style="color: #0033ff; font-weight: bold;">in</span> sourceInfo.variable<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
&nbsp;
                     <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>destination.<span style="color: #004993;">hasOwnProperty</span><span style="color: #000000;">&#40;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                         destination<span style="color: #000000;">&#91;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#93;</span> = <span style="color: #004993;">source</span><span style="color: #000000;">&#91;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#93;</span>;
                     <span style="color: #000000;">&#125;</span>
&nbsp;
                 <span style="color: #000000;">&#125;</span>
&nbsp;
                 <span style="color: #0033ff; font-weight: bold;">for</span> <span style="color: #0033ff; font-weight: bold;">each</span><span style="color: #000000;">&#40;</span>prop <span style="color: #0033ff; font-weight: bold;">in</span> sourceInfo.accessor<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                     <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>prop.@access == <span style="color: #990000;">&quot;readwrite&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                         <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>destination.<span style="color: #004993;">hasOwnProperty</span><span style="color: #000000;">&#40;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                             destination<span style="color: #000000;">&#91;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#93;</span> = <span style="color: #004993;">source</span><span style="color: #000000;">&#91;</span>prop.@<span style="color: #004993;">name</span><span style="color: #000000;">&#93;</span>;
                         <span style="color: #000000;">&#125;</span>
&nbsp;
                     <span style="color: #000000;">&#125;</span>
                 <span style="color: #000000;">&#125;</span>
             <span style="color: #000000;">&#125;</span>
             <span style="color: #0033ff; font-weight: bold;">catch</span> <span style="color: #000000;">&#40;</span>err<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                 ;
             <span style="color: #000000;">&#125;</span>
         <span style="color: #000000;">&#125;</span>
     <span style="color: #000000;">&#125;</span>
 <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>by <a href="http://evolverine.blogspot.com/2008/05/how-to-clone-duplicate-object-in.html">Evolvernie</a><br />
<!-- adman --></p>


<p>Related posts:<ol><li><a href='http://blog.another-d-mention.ro/programming/flex-actionscript/serialize-javascript-object-to-json/' rel='bookmark' title='Permanent Link: Serialize JavaScript object to JSON'>Serialize JavaScript object to JSON</a></li>
<li><a href='http://blog.another-d-mention.ro/programming/neural-networks-in-actionscript-3/' rel='bookmark' title='Permanent Link: Neural networks in ActionScript 3'>Neural networks in ActionScript 3</a></li>
<li><a href='http://blog.another-d-mention.ro/programming/how-to-identify-at-runtime-if-swf-is-in-debug-or-release-mode-build/' rel='bookmark' title='Permanent Link: How to identify at runtime if the swf is in debug or release mode/build'>How to identify at runtime if the swf is in debug or release mode/build</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>
