AS3 ByteArray to an object using depth copy

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  :

var copier:ByteArray=newByteArray();

Note: you need to import flash.utils.ByteArray;

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

ActionScript Code:


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:


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
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
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
var _copier:ByteArray=newByteArray();
} content

public class MyClass{
public var a:int=1;
public var b:int=2;
public function MyClass(){}

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.
 If you use the convert statement as  , The resulting value is aaa2  null.
分类:Flash 时间:2010-05-31 人气:234
blog comments powered by Disqus


  • Using pojo object data source for birt 2010-03-29

    Using pojo object data source for birt * Create a POJO object: package org.zsl.pojo; / ** * Define a simple POJO class * Casually defines several attributes which * / public class User ( private String name; private String phone; private String addre

  • Oracle objects - abstract data types. Variable array. Nested table. Object tables and object views 2010-02-23

    Oracle Database is RDBMS (relational database) is ORDBMS (object-oriented database). RDBMS: When reusing a function, will need to be compiled; ORDBMS: allows you to define object types; in a relational database to store complex business models; devel

  • dom object and jquery object 2008-11-12

    Difference dom object: Through the javascript in the geElementsByTagName or getElementById to get the elements of Node, so get dom element is the dom object. Dom object can use the javascript in the method var domObj = docuemnt.getElementById("id&quo

  • dom jQuery object and the object of mutual transformation 2010-07-06

    Just started learning jQuery, may be you could not tell what a moment jQuery object, which is a DOM object. The DOM object much to explain, we have come too much, concentrate on the following jQuery, and the conversion between the two. What is a jQue

  • [YUI source code analysis] YUI source of the initial simple 2010-08-04

    A general framework for the design to include many aspects, such as: support for standards and cross-browser event model functions. Come from the framework of the design aspects of this framework. To set out a framework for n-point design application

  • JS to create the object create the object (switch) 2010-09-26

    In JS, create an object (Create Object) is not exactly what we often said that the creation of class objects, JS objects in stress is a complex type, JS to create an object and the object of the visit is extremely flexible. JS object is a complex typ

  • Java: use of synchronized and the Lock object for the object lock (turn) 2010-11-02

    Transfer from: In a concurrent environment, sharing resources to resolve conflicts, you can consider using the lock mechanism. 1. Objects are automatically lock all the objects with a single lock. JVM is

  • QTP unable to identify pop up during the operation of the object and the object is not the only solution 2011-03-24

    One problem: Cannot Identify Object QTP runtime error that it could not identify the object, if your object in the object library which itself but also reported this error, which means that objects in the warehouse and procedures described in the des

  • Object.wait()与Object.notify()的用法详细解析 2014-11-27

    以下是对java中Object.wait()与Object.notify()的用法进行了详细的分析介绍,需要的朋友可以过来参考下 wait.notify和notifyAll方法是Object类的final native方法.所以这些方法不能被子类重写,Object类是所有类的超类,因此在程序中有以下三种形式调用wait等方法. wait();//方式1: this.wait();//方式2: super.wait();//方式3 void notifyAll()解除所有那些在该对象上调用wait

  • Object.getOwnPropertyNames和Object.keys区别 2013-10-09

    返回对象自己(非原型继承的属性)的属性名称,包括函数. 方法: Object.getOwnPropertyNames(object); Object.keys(object); 参数: object,如果不是object类型,则引发TypeError异常. 例子: 'use strict'; (function(){ if(!Object.getOwnPropertyNames){ console.log('浏览器不支持getOwnPropertyNames'); return; } //人类的

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.090 (s). 12 q(s)