Anonymous Getter Function
♥0 |
Line 43 |
Modified 2010-08-28 14:05:45 |
MIT License
archived:2017-03-20 13:37:17
ActionScript3 source code
/**
* Copyright 9re ( http://wonderfl.net/user/9re )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ngtC
*/
package {
import com.actionscriptbible.Example;
public class FlashTest extends Example {
public function FlashTest() {
var square:ExtObject = new ExtObject;
square.size = 3;
square.setAnonymousGetter('area', function ():int {
return this.size * this.size;
});
trace('the size of the square : ' + square.size);
trace('its area : ' + square.area);
}
}
}
import flash.utils.Proxy;
import flash.utils.flash_proxy;
dynamic class ExtObject extends Proxy {
protected var _anonymousGetters:Object;
protected var _properties:Object;
public function ExtObject() {
_anonymousGetters = {};
_properties = {};
}
override flash_proxy function callProperty(methodName:*, ... args):* {
switch (methodName.toString()) {
case 'setAnonymousGetter':
_anonymousGetters[args[0]] = args[1];
break;
default:
break;
}
return;
}
override flash_proxy function getProperty(name:*):* {
if (name.toString() in _anonymousGetters) {
return _anonymousGetters[name].apply(this);
}
return _properties[name];
}
override flash_proxy function setProperty(name:*, value:*):void {
_properties[name] = value;
}
}