如果你想保留括号表示法,你仍然可以使用Proxy类,包装一个真正的字典。这里是使用Proxy类的实现,但这里我没有使用弱字典,因为它可能会很棘手,因为“密钥”可能会被垃圾收集,而你不会意识到这一点。当然性能操作(添加,删除,...)也会低于真正的字典。
这里是现场测试:http ://wonderfl.net/c/dstz
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"]);