Using Functions, Properties, and Keywords in ActionScript 3
WITH UNLIMITED, OPTIONAL parameters, ActionScript 3 functions provide flexibility for unexpected additions. You can also control the access to the functions you create with new attribute keywords. If you have ever used the static keyword to create a pseudoconstant in ActionScript 2, you are a resourceful developer, but you’ll be happy to know that you can now create true constants.
In this chapter you’ll learn the differences between function arguments and the new ...(rest) keyword, attribute keywords such as public and private, and the new protected keyword, as well as how to define constants.
Often confused with function parameters, arguments are actually the values of function parameters. In ActionScript 2, function parameters are defined explicitly in a function definition. There are two ways to retrieve the value of a function parameter: either through direct access to an explicit parameter variable or through the arguments object. In ActionScript 3, it’s possible to use both these options, but a new keyword named ...(rest) can also define the optional parameters within a function definition. The ...(rest) keyword provides more information and offers more control over optional function parameters, and is recommended over the arguments object.
ActionScript 2 offers a number of ways to access the value of function parameters. You can access the explicit variables defined as function parameters or the values in the arguments object.
The values within the arguments object are stored as an Array. You can access the array of values in the arguments object in the same order they were passed into the function, by iterating through the array or accessing the values in the arguments object by index. In the previous sample, 1 is the first value in the arguments object, and 2 is the second value in the arguments object. Since the arguments object is structured as an Array, determining how many parameter values were passed into a function is simple. As with any array, the length property determines the number of argument values and can be used to iterate the arguments object.
The length property is important because you can’t simply look at how many parameters were defined in the function to determine the number of argument values. The arguments object can contain more values than the number of explicit function parameters. The following example shows how you can use the length property to iterate the values in the arguments object.
Although the arguments object is available in ActionScript 3, a new keyword named ...(rest) is the recommended solution for optional parameters. As with the arguments object, the ...(rest) keyword provides a way to pass any number of optional parameters to a function. One of the differences with the ...(rest) keyword is that you define it as a parameter in a function definition.
The ...(rest) keyword also lets you name the list of parameter values. Giving the parameters a name helps identify what kind of values the function is expecting and thus clarifies your code.
The ...(rest) keyword can also be added as a function parameter after defining explicit parameters. The catch is to remember that the ...(rest) keyword can be defined only after explicit parameters because required parameters must come before optional ones. Also, the ...(rest) parameter is populated only when the number of arguments exceeds the number of explicit parameters. And unlike the arguments object, the ...(rest) keyword contains only extraneous function parameters and not explicit parameters.
Although the ...(rest) keyword is similar to an Array, it is only a comma-delimited list of mixed values and does not include any of the properties or methods of an Array object, with the exception of the length property.