Recently I am studying “Essential Actionscript 3.0” by Colin Mook. I am in the first chapter itself and informations are already there to digest. I simply love the Mook’s books. Previously it was for AS2 and that was too good, now this is an excellent addition. This writing simply brings the books pages to here with my own edits 🙂 .
Ok, straight to the point, how to write a method, inside a class, which can handle “multiple” or better word as per Mook is “unknown” number of parametrs ?!! The answer is “…(rest)” parameter. This parameter defines an Array to hold any number of arguments passed to a given method. It can be used on its own or in combination with named parameters.
Using the parameter alone :
Now try to call the function with some new number of parameters as
The result will be obviuos.Lets try to use the “…(rest)” parameter with a named parameter.
I have simply added “sam” before the function name, instead of my name “saumya” 🙂 . Ok, now if you look at the result it will give you “14”.Lets the call the function with the desired number of parameter, which is 1.I mean you must pass one parameter to successfully call this function.So the next call will be
This will result in “20”, but what happens if we call it without any parameter as
The compiler throws an error saying “Incorrect number of arguments.Expected 1.”
When used with other parameters, the “…(rest)” parameter must be the last parameter in the parameter list.That means, we can not write
//code to be executed
For me, its just WOW.
Cheers to Mook. I will put things like this as i proceed through the book.