0

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...

1
  • This is basically a way to make things very opaque and difficult to debug. You do not create any dynamic methods but simply add reference to existing ones. You also do not provide any overloading functionality as you claim. At the end of the day the only functionality you achieve is having loose class methods that can escape completely compile time checking. Commented May 18, 2015 at 15:47

2 Answers 2

1

SomeClass should be dynamic.

public dynamic class SomeClass {

A dynamic class defines an object that can be altered at run time by adding or changing properties and methods.

Sign up to request clarification or add additional context in comments.

Comments

0

You can create a property that holds the Functions. public class SomeClass {

private var functions:Object = {};

public function SomeClass()
{
}

public function addFunction(name:String, function:Function):void
{
    functions[name] = function;
}

}

This has the advantage that you don't have to make your class dynamic, which is very undesirable, because a dynamic class is harder to be error checked at compile time.

You should still do plausibility checks.

Compare this to the EventDispatcher class, which allows you to add "methods" to it without being dynamic.

5 Comments

What I was trying is to achieve is overload effect in as3, so I wanted to call my added function directly calling it, not with help of other function, ex: mc.myAddedMethod(someParamsHere) in your code I will need extra method ex: public function callMethod(methName:String):void {} which calls needed function... thanks for reply
You are using names to identify your methods, which means that you still cannot overload one method name. What you are doing has nothing to do with overloading. you basically just want to add methods to an object at run time. If you want to call methods directly, take a look at the decorator pattern, it allows you to extend a class at run time but with type safety.
I didn't write all the code... I achieved that effect mc.addMethod("mm", function(a:int):void { trace(a); } ); mc.addMethod("mm", function(a:int,b:int):void { trace(a,b); } ); mc.mm(1); mc.mm(2,3); this works for me
@Ratty Could you please write all the code then? (edit your original question) An object cannot have two properties with the same name. You are likely overwriting the first function with the second one. It works because trace can take a variable amount of parameters. Change trace(a,b); to trace("two parameters"); and trace(a); to trace("one parameter"); and see which function is actually called.
@Ratty I was under the impression that your comments are related to my answer. Of course if you differentiate between functions this way you do not run into problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.