简体   繁体   中英

flex/actionscript question

var n:Number = 0;
[1,2,3].forEach(function (x):void {
    n+=x;
});

how to do it in flash? Flash throws error "function called with 3 arguments..." i need only one argument here!

The Array.forEach() waits a function as first parameter which looks like this:

    function callbackFunc ( item:*, index:int, array:Array ) : void

You have to declare in your function all of the three parameters. So your stuff should look like this:

    var n   : Number = 0;
    var arr : Array  = [1,2,3];

    arr.forEach(function (item:*, index:int, array:Array):void {
        n+=index;
        trace( "n: " + n )
    });

You can't use the [1,2,3].forEach form in actionscript, because the compiler will see it as a bad metadata and throws an error.

I am assuming your [1,2,3] is an array. In that case do it like this.

var d:Array = [1,2,3,4,5];
var v:int;
var n:Number =0;

for each(v in d )
{
    n+=v;
    trace(n);   
}

This outputs: 1 3 6 10 15

Good luck: :)

Is this what you mean?

var n:Number = 0;
var ar:Array = [1,2,3];

for each(var i:Number in ar)
{
    n += i;
}

trace(n);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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