AS3 ByteArray to an object using depth copy

sponsored links
Use ActionScript3 new in ByteArray class (flash.utils.ByteArray) can create an object of deep copy. "Depth" means that you can copy an object (object) of all references, which means that if you copy an array containing an object, then the array of objects are copied (not quoted). Here is a clone method:

ActionScript Code  :

functionclone(source:Object):*{
var copier:ByteArray=newByteArray();
copier.writeObject(source);
copier.position=0;
return(copier.readObject());
}
Note: you need to import flash.utils.ByteArray;

To use this method, you can use the following code:

ActionScript Code:

newObjectCopy=clone(originalObject);
Although this method you can copy the depth of the object, but it will not copy with the class type definition. So, if Charlie need to copy a MyClass instance, copy the object will not be MyClass type. This method is best to use ordinary objects.

Here are some of my test code:

i
mportflash.utils.ByteArray;
flash.utils.getQualifiedClassName;
var a1:Array=[1,2,3];
var a2:Array=clone(a1);// Clone a1, a2 is saved in  a1 Of copies, and  a1 There is no relationship between the  
var a3:Array=a1;// Because of the a1 is an array  , So the a3 is a reference  a1
a1.push(4);// Change the value of the a1  
trace(a1);// Output  1,2,3,4
trace(a2);// Output  1,2,3
trace(a3);// Output  1,2,3,4
trace("===============");
varaa1:Array=[1,2,3];
varaa2:Array=[11,22,aa1]//aa2 An element is in the  aa1
varaa3:Array=clone(aa2);// Clone aa2, is saved in aa3  aa2 A copy, of aa2  aa1 Also copies  
trace(aa1);// Output  1,2,3
trace(aa2);// Output  11,22,1,2,3
aa1.push(4);// Modify the value of aa1  
trace(aa2);// Output  11,22,1,2,3,4
trace(aa3);// Output  11,22,1,2,3
trace(getQualifiedClassName(aa2));// Output  Array
trace(getQualifiedClassName(aa3));// Output Array, this description  Array You can maintain a clone of a clone of the result Array type invariant  
trace("===============");
var aaa1:MyClass=newMyClass();// Build a custom type  
var aaa2=clone(aaa1);// Clone custom types  
trace(aaa2.a);// Output  1
trace(getQualifiedClassName(aaa2));// This indicates that the output Object,  aaa2 The type of MyClass lost  
functionclone($source:Object):*
{
var _copier:ByteArray=newByteArray();
_copier.writeObject($source);
_copier.position=0;
return_copier.readObject();
}

MyClass.as content

package{
public class MyClass{
public var a:int=1;
public var b:int=2;
public function MyClass(){}
}
}
Note:
Can not be cast for the aaa2 type, if you use the following statement:

var aaa2=MyClass(clone(aaa1));
 It is an error  :

TypeError:Error#1034: Cast type failed  : Cannot add  Object@138c0341 Converted to  MyClass.
atDeepObjectCopieswithByteArray_fla::MainTimeline/DeepObjectCopieswithByteArray_fla::frame1()
 If you use the convert statement as  , The resulting value is aaa2  null.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of AS3 ByteArray to an object using depth copy

  • hibernate in the get method and the difference between load method

    hibernate in the get method and the load method of the fundamental difference is: If you use the load method, hibernate consider the corresponding object id (database records) are in the database must exist, so it can be assured that the use, it can be as

  • Verification Code Rails implementation continued

    http://sanyking.javaeye.com/blog/120339 Connect with the address above on this website are one of the other way regarding the implementation Verification Code article, my article are based on the above completed, and the world is one big copy article, loo

  • Play flash page 3 (javascript script swfobject.js1)

    / ** * SWFObject v1.5: Flash Player detection and embed - http://blog.deconcept.com/swfobject/ * * SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License: * Http://www.opensource.org/licenses/mit-license.php * * / if (typeof deconc ...

  • javascript built-in functions Info

    48. In the old browsers do not perform this JS :<!-- //--> 49. To quote a document-style JS: <script type="text/javascript" src="/blog/aaa.js"> </ script> 50. Specified in the script does not support the browser displ

  • Imitation Java type HashMap implementation of JavaScript

    Imitation Java type HashMap implementation of JavaScript

  • Javascript object inheritance

    Javascript object inheritance

  • Depth understanding of the eval function in javascript

    http://wanyij.blog.51cto.com/46570/43794 In this paper, the discovery of an appropriate title is not so easy, huh, huh, so in this note under the first two purposes of this article: (1) introduction of the eval function in javascript usage (2) how to func

  • Javascript Object Model

    Javascript Object Model

  • JUnit Unit Testing Summary

    Read some of the unit test article, from the article in the interception of a number of information, which could be considered in the learning process, a summary of it! See future! Unit testing code is not used to prove you are right, but in order to ...

  • Hibernate II Study Notes

    11. Many-to-many Of many that can be converted to two one-to-many <set name="students" table="teacher_student"> <key column="techer_id"/> <many-to-many column="student_id"/> </set> many-to-many data only from one end of the mainten

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries