当前位置: 代码迷 >> Web前端 >> Object跟ObjectProxy的互转
  详细解决方案

Object跟ObjectProxy的互转

热度:287   发布时间:2012-10-06 17:34:01.0
Object和ObjectProxy的互转

本文简单介绍Object和ObjectProxy的互转

1 Object2ObjectProxy

private function object2ObjectProxy():void{
        //普通的Object
        var obj:Object = {"name":"swflash"};
        var objProxy:ObjectProxy = new ObjectProxy(obj);
        trace(obj.name);
        trace(objProxy.name);
        //数组
        var array:Array =[{"name":"swflash"}];
        var arrayProxy:ObjectProxy = new ObjectProxy(array);
        trace(array[0]["name"]);
        trace(arrayProxy[0]["name"]);
}
?

输出结果如下:

?写道
swflash
swflash
swflash
swflash
?

可见ObjectProxy的使用方法和普通的Object一样。

?

2 ObjectProxy2Object

?

ObjectProxy类包含object,不过是只读的。要获取ObjectProxy中的Object属性需要费一点周折。

//方法1

private function objectProxy2Object():void{
        //普通的Object
        var obj:Object = {"name":"swflash"};
        var sourceObj:Object = {"name":"swflash"};
        var objProxy:ObjectProxy = new ObjectProxy(sourceObj);
        //直接获取,会自动import mx.utils.object_proxy;
        var targetObj:Object = objProxy.object_proxy::object;
        trace(targetObj["name"]);
        trace(obj == sourceObj);
        trace(sourceObj == targetObj);
}
?

输出:

?写道
swflash
false
true
?

可见获取到的实例为原有的Object,而不是备份。

?

//方法2

private function objectProxyTOObject():void{
        var sourceObj:Object = {"name":"swflash"};
        var objProxy:ObjectProxy = new ObjectProxy(sourceObj);
       //使用下面语句需要使用名次空间
       //import mx.utils.object_proxy;
       //use namespace  object_proxy;
        var targetObj:Object = objProxy.object;
        trace(targetObj["name"]);
        trace(sourceObj == targetObj);
}
?

输出:

?写道
swflash
true
?
  相关解决方案