AS3:如何深复制对象 [英] AS3: How to Deep Copy an Object

查看:235
本文介绍了AS3:如何深复制对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我需要复制到我的共享对象数据的对象。 问题是,共享对象的数据属性是只读的,所以我不能复制我的'源'对象,并将其分配给共享对象的数据,我必须让他们在这样一个副本:

i have an object that i need to copy to my SharedObject data. The problem is that the data property of shared object is read-only, so i can't clone my 'source' object and assign it to the shared object data, i have to make a copy of them in this way:

var so: SharedObject = SharedObject.getLocal("appData");
copyObject(sourceObj, so.data);
so.flush();

和复制方法:

public static function copyObject(sourceObject:Object, destinationObject:Object):void{
    // this would be the code that i need
}

也有一点,我的对象是对象的属性,所以它有内部的 N 的对象leves。这就是为什么我不能简单地做一个对每个并在第一级分配所有的属性,所以我需要的是做一个深拷贝,可能是递归的。我尝试了几个小时,使这个copyObject方法没有成功。也是我在互联网上搜索,但我没有找到一个适合我的任何对象副本。

Also have in mind that my object has properties that are objects, so it has inside n leves of objects. That is why i can't simply make a for each and assign all properties on the first level, so what i need is to make a DEEP copy, probably recursive. I tried for hours to make this copyObject method with no success. Also i've searched on the internet but i didn't find any object copy that suits me.

是否有人可以帮我这个方法是什么?我真的一个preciate了!

Can someone please help me with this method? I would really apreciate it!

感谢您的帮助!

推荐答案

对于复杂的对象,我会用RegisterClassAlias​​:

For complex objects I would use RegisterClassAlias:

import flash.net.registerClassAlias;
registerClassAlias("YourClassName", YourClassName);
var so:SharedObject = SharedObject.getLocal("objectName");
so.data.yourData = YourClassName.instance;
so.flush();

有关用简单的数据(原语包括数组)深层次的简单对象类型我会简单地使用JSON.stringify()和JSON.parse()回读时的数据。

For simple Object type with deep level of simple data (primitives including arrays) I would simply use JSON.stringify() and JSON.parse() when reading back the data.

这篇关于AS3:如何深复制对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆