摘自:http://www.darronschall.com/weblog/archives/000148.cfm
1. the object is an instance of the class
function copyObject(obj) {
var copy = (obj instanceof Array) ? [] : {};
for(var i in obj) {
var item = obj[i];
switch(item instanceof Array || item instanceof Object) {
case true:
copy[i] = copyObject(item);
break;
default:
copy[i] = item;
}
}
return copy;
}
/*
//Sample usage:
var obj:Object = new Object();
obj.color = "red";
obj.count = 5;
var obj2:Object = copyObject(obj);
obj2.color= "blue"
trace(obj.color);
trace(obj2.color);
*/
2. the object is not an instance of the class
import mx.utils.ObjectCopy;
var dog1:Dog = new Dog("Yelowdog");
var dog2:Dog = Dog( ObjectCopy.copy(dog1) );
dog2.name = "Yellowdog Clone";
trace( dog1.speak() );
trace( dog2.name );
trace( dog2.speak() );
trace( dog2 instanceof Dog );