forked from as3/as3-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclone.as
More file actions
39 lines (37 loc) · 1.47 KB
/
clone.as
File metadata and controls
39 lines (37 loc) · 1.47 KB
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
package utils.object
{
import flash.net.registerClassAlias;
import flash.utils.ByteArray;
import flash.utils.getQualifiedClassName;
/**
* Creates a duplicate of the source object by storing it in a ByteArray and reading it back in,
* The object will return untyped so you may need to cast it to the desired class.
* AMF rules apply. Check out the documentation for more info.
* NOTE: This will not work if the object has any required parameters in the constructor.
*
* @throws flash.errors.ArgumentError If the source's constructor requires any parameters.
*
* @example
* <listing version="3.0">
* var example:Example = new Example();
* var clone:Example = Cloner.clone(example) as Example;
* </listing>
*
* @see flash.net#registerClassAlias()
*
* @param source The object to duplicate.
* @param autoRegisterClassAlias Determines whether the class will be registered before cloning.
* Defaults to true, though advanced users may wish to register class aliases manually.
* @return * A duplicate of the source object.
*
* @author Mims Wright
*/
public function clone(source:Object, autoRegisterClassAlias:Boolean = true):* {
// automatically register the class so that the clone retains its class.
if (autoRegisterClassAlias) registerClassAlias(getQualifiedClassName(source), source.constructor as Class);
var copy:ByteArray = new ByteArray();
copy.writeObject(source);
copy.position = 0;
return copy.readObject();
}
}