How ca开发者_运维百科n I define functions that can receive unknown/variable amount of parameters?
It is called rest parameter, Use this:
function getItems(...rest):void
{
// ... logic goes here
}
Inside the function you deal with rest as the name of the array.
When creating a rest parameter you should keep these in mind:
- Rest parameters are untyped. It is up to you to validate any special type requirements as you loop through the rest parameter array.
- Rest parameters must be at the end of a method's parameters.
- The rest parameters must have ... in front of it, but the variable name can be anything.
private function someFunction(...args) : void
{
for(var i : int = 0; i < args.length ; i++)
{
trace(i, args[i]);
}
}
精彩评论