简体   繁体   English

在类as3中动态创建函数

[英]Dynamically create function in class as3

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: 我基本上想做的是在实例化类后在类中动态创建函数...我真的不知道是否有可能在as3中实现这一点...下面是一些示例:

    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 ReferenceError:错误#1056:无法在SomeClass上创建属性myMethod

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 @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... 现在,我试图使用相同数量的参数来添加两个或多个函数,但是此参数可以是不同的类型,并且我有一些改进...

SomeClass should be dynamic. SomeClass应该是动态的。

public dynamic class SomeClass {

A dynamic class defines an object that can be altered at run time by adding or changing properties and methods. 动态类定义了一个可以在运行时通过添加或更改属性和方法来更改的对象。

You can create a property that holds the Function s. 您可以创建一个包含Function的属性。 public class SomeClass { 公共类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. 这样做的好处是不必使类成为dynamic类,这是非常不希望的,因为动态类在编译时很难进行错误检查。

You should still do plausibility checks. 您仍然应该进行真实性检查。

Compare this to the EventDispatcher class, which allows you to add "methods" to it without being dynamic . 将此与EventDispatcher类进行比较,该类使您无需dynamic就可以向其添加“方法”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM