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 开发者_JAVA百科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);
精彩评论