What I am basically trying is to create function in class dynamically after class is instantiated... I don't really know if it's possible to achieve this in as3... here are some examples:
var mc:SomeClass = new SomeClass();
mc["myMethod"] = function():void {};
or
public function SomeClass()
{
}
public function addMethod(methName:String, func:Function):void
{
pushMeth(func);
this[methName] = getMeth();
}
both examples are the same...
mc["myMethod"] = function():void {};
or
this[methName] = getMeth();
throws:
ReferenceError: Error #1056: Cannot create property myMethod on SomeClass
so can you give my any suggestions how can I achieve this or what problem is in my code, or can I achieve this effect at all?
EDIT!!!
full working code for @null
public dynamic class OverloadExample
{
private var _meths:Object = {};
public function OverloadExample()
{
}
public function addMethod(methName:String, func:Function):void
{
pushMeth(func);
this[methName] = getMeth;
}
private function pushMeth(func:Function):void
{
_meths[func.length] = func;
}
private function getMeth(...args):void
{
var l:int = args.length;
if (_meths[l]) {
_meths[l].apply(this,args);
}
}
}
var o:OverloadExample = new OverloadExample();
o.addMethod("mm", function(a:int):void { trace("one"); } );
o.addMethod("mm", function(a:int,b:int):void { trace("two"); } );
o.mm(1);
o.mm(2,3);
NOTE: this is just an example of how to achieve overload effect...
Now I'm trying to be able to addMethod two or more function with the same amount of arguments, but this arguments can be different types and I have some improvements...