SO dictionary proxy

by pleclech forked from forked from: flash on 2011-2-25 (diff: 39)
♥0 | Line 56 | Modified 2012-09-14 19:58:52 | MIT License
play

ActionScript3 source code

/**
 * Copyright pleclech ( http://wonderfl.net/user/pleclech )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dstz
 */

// forked from pleclech's forked from: flash on 2011-2-25
// forked from pleclech's flash on 2011-2-25
package {
    import flash.utils.getTimer;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        public function FlashTest() {
             if (stage) _init()
            else addEventListener(Event.ADDED_TO_STAGE, _init)
        }
        import flash.text.TextField;
        import flash.events.Event;

        private var tf:TextField=new TextField()
        private function trace(...args):void{
            tf.appendText(args.join(", ")+"\n")
        }
        private function _init(e:Event=null):void {
            removeEventListener(Event.ADDED_TO_STAGE, _init)
            addChild(tf)
            tf.autoSize="left"
            tf.background=true
            test()
       }
       private function test():void{
           var dict:MyDict = new MyDict();
           dict[1] = "one";
           dict["foo"] = "bar";
           
           trace(dict.size, dict[1], dict["foo"]);
           
           delete dict[1];
           trace(dict.size, dict[1], dict["foo"]);       }
    }
}

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

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;
    }
}

Forked