开发者

Custom ActionScript classes: Syntax sugar

开发者 https://www.devze.com 2022-12-21 21:18 出处:网络
Because of some limitations (example) in the built-in ActionScript 3 Dictionary class I\'m looking to build a wrapper which adds such things. Is it possible to keep the syntax below for my custom clas

Because of some limitations (example) in the built-in ActionScript 3 Dictionary class I'm looking to build a wrapper which adds such things. Is it possible to keep the syntax below for my custom class, and if so how?

var dic:MyDic = new MyDic();
dic[stuffy] = so开发者_开发技巧meObject;


Yes you can using Proxy function but with performance overhead:

import flash.utils.Dictionary;
import flash.utils.Proxy;
import flash.utils.flash_proxy;

public class MyDict extends Proxy {
    private var _size:int = 0;
    private var _dict:Dictionary = new Dictionary();

    public function get size():int {
        return _size;
    }

    flash_proxy override function getProperty(name:*):* {
        return _dict[name];
    }

    flash_proxy override function setProperty(name:*, value:*):void {
        if (!_dict.hasOwnProperty(name))
            _size ++;
        _dict[name] = value;
    }

    flash_proxy override function deleteProperty(name:*):Boolean {
        if (_dict.hasOwnProperty(name)) {
            _size --;
            delete _dict[name];
            return true;
        }
        return false;
    }
}

var dict:MyDict = new MyDict();
dict[1] = 2;
dict["foo"] = "bar";
trace(dict.size, dict[1], dict["foo"]);

delete dict[1];
trace(dict.size, dict[1], dict["foo"]);
0

精彩评论

暂无评论...
验证码 换一张
取 消