AS3 arguments

前端 未结 3 1919
北海茫月
北海茫月 2021-01-14 22:47

Why do you think the code below does not work? What would you change/add to make it work?

Any help is appreciated..

function TraceIt(message:String,         


        
相关标签:
3条回答
  • 2021-01-14 23:22

    Ok, here is the solution.. after breaking my head : )

        function TraceIt(message:String, num:int)
        {
            trace(message, num);
        }
    
        function aa(f:Function=null, ...args):void
        {
            var newArgs:Array = args as Array;
            newArgs.unshift(f);
            bb.apply(null, newArgs);
        }
    
        aa(TraceIt, "test", 1);
    
        var func:Function = null;
        var argum:*;
    
        function bb(f:Function=null, ...args):void
        {
            func = f;
            argum = args as Array;
            exec();
        }
    
        function exec():void
        {
            if (func == null) { return; }
            func.apply(this, argum);
        }
    

    This way, you can pass arguments as variables to a different function and execute them..

    Thanks to everyone taking the time to help...

    0 讨论(0)
  • 2021-01-14 23:25

    When TraceIt() eventually gets called, it's being called with 1 Array parameter, not a String and int parameters.

    You could change TraceIt() to:

    function TraceIt(args:Array)
    {
         trace(args[0], args[1]);
    }
    

    Or you could change exec() to:

    function exec()
    {
         func.apply(null, argum[0].toString().split(","));
    }
    

    ...as it appears when you pass "test", 1, you end up with array whose first value is "test,1". This solution doesn't work beyond the trivial case, though.

    0 讨论(0)
  • 2021-01-14 23:30

    Change your bb function to look like this:

    function bb(f:Function, args:Array):void
    {
        func = f;
        argum = args;
        exec();
    }
    

    As you have it now, it accepts a variable number of arguments, but you are passing in an array(of the arguments) from aa.

    0 讨论(0)
提交回复
热议问题