当前位置: 首页 > 工具软件 > Object-proxy > 使用案例 >

Object和ObjectProxy的互转

令狐经武
2023-12-01

本文简单介绍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
 
 类似资料: