The Arguments Class

Each time a function is invoked, an Arguments object is created automatically and can be referenced using the local variable arguments, which is automatically included within the scope of the function. Basically, arguments are the parameters you define when you invoke any function. The arguments variable of any function is more like an array (refer to Chapter 10, "Arrays," for more on arrays). As with an array, you can call specific elements as well as the number of total elements. The arguments variable can be used to get information about any and all of the parameters being passed to that function.

Let's start with the number of arguments in a given function. Gathering this information is as easy as using the length property of the arguments object.

The length Property

The length property of the arguments object found in all functions returns a value that represents the number of parameters a user has defined when a function is invoked. The generic template looks like this:

 function functionName(parameters:ParameterType):Void{     //code to be run in the function     arguments.length; } 

As you'll notice, you must use the length property as well as the arguments object inside the function. (The length property is also a property of arrays as well as a property of strings and can be used outside a function only in that context.)

Now let's see this property in a real example. Create a function with two basic parameters and trace the length of the arguments, like so:

 function myFunction (x:Number,y:Number):Void{     trace (arguments.length); } //now run the function myFunction(5,6); //output: 2 

The function runs, and the number of parameters are displayed in the output window. However, as mentioned before, the length property returns the number of arguments when the function is invoked, not created. To see what that means, here is an example:

 //create a function with two parameters function myFunction(x:Number,y:Number):Void{     trace(arguments.length); } //now invoke the function, but add a parameter myFunction (5,6,7); //output: 3 

Now that you have seen how to find the number of arguments, the next step is to pull individual arguments out of the entire set.

To call individual arguments, you use the arguments object and a numerical value held in brackets. The generic template looks like this:

 function functionName(parameters:ParameterType):Void{     //code to be run in the function     arguments[N]; } 

With this generic template, place a number (N) in the brackets that represent the argument in that position. However, note that arguments begin counting at 0 instead of at 1, like arrays, so the first element looks like this:


Here is an example using this method of pulling individual arguments:

 //create a function with three parameters function myFunction(x:Number,y:Number,z:Number):Void{     trace(arguments[1]); } //now invoke the function myFunction(2,4,6); //output: 4 

Because arguments begin counting at 0 instead of at 1, the second argument is labeled [1] instead of [0].

Now, using this method combined with the length property, you can create some amazing code. Let's take a look at an example that creates a function that adds all numbers placed as parameters:

 //first create a function with no parameters function addArgs():Void{ //now use a loop statement to total the argument values     var numTotal:Number = 0;     for(var i:Number = 0; i<arguments.length; i++){         numTotal += Number(arguments[i]);     } //display the total in the output window     trace (numTotal); } //now invoke the function with as many addArgs(1,2,3); //This can also work with combining strings. //first create a function with no parameters function combineStrings():Void{ //create a variable that will hold a string literal space     var space:String = " ";     var total:String = ""; //now use a loop statement to combine the strings     for(var i:Number = 0; i<arguments.length; i++){ //now convert each argument to a string for consistency         total +=arguments[i].toString()+space;     }     trace (total); } //now invoke the function with as many combineStrings("Flash","8 Professional","Unleashed"); //output: 6 // Flash 8 Professional Unleashed 

Another great use of this technique involves creating an array that can hold the arguments outside of the function, because as previously stated, the arguments object cannot be used outside the function. Here's an example:

 //first create a function with no parameters function createArray():Array{ //create an array to hold the arguments     var myArray:Array = new Array();     for(var i = 0; i < arguments.length; i++){         myArray.push(arguments[i]);     }     return myArray; } //set a variable equal to the returned array var argsArray:Array = createArray(1,2,3,"One","Two"); //display new array in output window trace(argsArray); //output: 1,2,3,One,Two 

These are just a few examples of using the length property combined with pulling individual arguments. There are two more properties of the Arguments object, and they are the callee function, which refers to the function being called, and the caller property that refers to the Arguments object of the calling function.

So far, you have seen functions used as easily repeatable code that can be changed, as needed, by using parameters. However, you can also use them for more than just actions because they can also be used as objects.

Functions as Objects

Using a function as an object may seem a bit unorthodox, but this actually greatly increases the usability of a function. To create a function object, you assign it without the parentheses and use it like an expression. Also, because the function is an object, you can move it around like any other type of data.

Following is an example using the built-in function trace() (built-in functions are discussed in the "Functions Built In to Flash" section, later in this chapter) to send a property of the object to the output window:

 //First, create the function function myInfo ():Void{     trace(; } //Assign a property called name to the function = "David"; //Run the function myInfo(); //output: David 

Assigning properties to functions is easy because of the built-in function object. You can even assign multiple properties to your functions. Now, suppose you would like to see a list of all the properties in a function. You can use the for in loop statement to pull properties of objects. (For more on loop statements, see Chapter 11, "Statements and Expressions.")

To use the for in loop statement, place the keyword for ahead of an opening parenthesis. Then place the keyword var followed by your variable's name (in this case, functionProp). Then place the keyword in followed by the name of the object (the function's name, in this case). Finally, place a closing parenthesis, then opening and closing curly brackets enclosing the script you want to run while the loop looks through the properties, and finally, a closing curly bracket. Here's what it looks like:

 //First, create the function function myInfo ():Void{ } //Now assign properties to it myInfo.fName = "David"; myInfo.lName = "Vogeleer"; myInfo.age = "25"; myInfo.location = "Virginia"; //now use the for in statement to look through our properties for(var functionProp in myInfo){     trace("The property "+functionProp+" equals "+myInfo[functionProp]); } //output: The property location equals Virginia //            The property age equals 25 //            The property lName equals Vogeleer //            The property fName equals David 

Notice that, in this example, we not only called the name of the property but also the value by using the function's name and the property we wanted to call in brackets. Because we used a dynamic variable, we did not have to put it in quotes. However, to call a single property of a function using brackets, you must place the name of the property in quotes, as shown in the following code:

 //First, create the function function myInfo ():Void{ } //Now assign the property to it myInfo.age = "25"; trace(myInfo["age"]); //output: 25 

Let's take it another step forward by creating multiple functions with multiple properties. Then we'll store each function in an array. (As mentioned earlier, arrays are covered in greater detail in Chapter 10.) After that, we'll call all the properties of all the functions to be displayed in the output window, and because we stored the functions in the array as objects, we can invoke the functions by using the array.

First, we'll create the functions we need and, in the first one, store the TRace function we'll use at the end of the code. After that, we'll assign the properties to the functions.

Then we'll create the array and store the functions in the array as objects. Finally, we'll run a script that looks through each element of the array as well as displays each property in each element.

After that, we'll invoke our first function using the array as a shortcut. Here's the code:

 //First, create the functions function myInfo ():Void{ //This script is for later use     trace("Traced from myInfo"); } function flashInfo ():Void{ } //Create the properties for each function myInfo.fName = "David"; myInfo.age = "25"; flashInfo.version = "8 Pro"; flashInfo.player = 8; //Now create the array to hold the functions as objects var functionArray:Array = new Array(); //Place the functions in the array functionArray[0] = myInfo; functionArray[1] = flashInfo; /* Finally we create the script to search through the array and trace all of our properties with their values */ for (var myElement in functionArray){     for (var functionProp in functionArray[myElement]){         trace("Property "+functionProp);     } } //and because the function is stored as an object in the array //we can call it using the array functionArray[0](); //output: Property player //        Property version //        Property age //        Property fName 

In the preceding code, we used only two functions and two properties for each function. You can, however, increase both, and the script will still function properly.

Now that you have seen functions used as repeatable sets of code and as objects, let's move on to using them as methods to be used in conjunction with other objects.

Functions as Methods

Before we get into using functions as methods, we need to review what a method is. A method is a function used in conjunction with an object. A method can perform a desired task on an object or can be used to gather information from that object.

We call methods just like we call functions, using parentheses; however, methods are attached to objects using dot syntax. Here is a generic template for calling a method:


The preceding template is just for calling a method; to assign a method, you drop the parentheses, like this:


Now that you have the generic templates to go by, let's start creating a few methods. First, we'll place a simple TRace function within a function we create. Then we'll create a generic object, called gObject, and assign a property of gObject to our function. Then we'll invoke it by using the entire name, including the property. Here's the code:

 //First, the function function myFunction():Void{     trace("Flash 8 Professional Unleashed"); } //Then the generic object var gObject:Object = new Object(); //Now the property of the object gObject.title = myFunction; //Now invoke the method gObject.title(); //output: Flash 8 Professional Unleashed 

Using a method is stronger than using a function when dealing with an object in that the method can manipulate the object itself. Functions can also get information from objects when used as methods. Let's start by looking for a single property in an object and then have the function return its value instead of tracing it directly. Then we'll trace the method. Here's the code:

 //Here, create the function, and have it look for the property function getAge ():Number{     return(this.age); } //create the object var gObject:Object = new Object(); //Now add two properties to our object gObject.age = 25; gObject.ageVerify = getAge; trace(gObject.ageVerify()); //output:25 

You can also directly define the method of the object like this:

 //create the object var gObject:Object = new Object(); //Now add two properties to our object gObject.age = 25; //create the method gObject.ageVerify = function():Number{     return(this.age); } trace(gObject.ageVerify()); //output:25 

That was a lot of work for not a big payoff. We could have simply traced the property itself. Therefore, let's build on the previous example and combine multiple properties in an expression that we may want to use again and again.

In this example, we'll create an object called myRec. Then we'll create a function that gets the area and the perimeter of the object. The idea is that no matter how often the properties we use change, the function will perform the same calculation on our object. Here's the code:

 //First, create the object var myRec:Object = new Object(); //Now add a property and two methods to our object myRec.w = 5; myRec.h = 10; myRec.getArea = getRecArea; myRec.getPerim = getRecPerim; //Here, create the functions function getRecArea():Number{     return this.w * this.h; } function getRecPerim():Number{     return (this.w * 2) + (this.h * 2); } //Finally, invoke the methods trace(myRec.getArea()); trace(myRec.getPerim()); //output: 50 //        30 

The preceding code shows that methods can be quite powerful when used in an expression. You can change the value of w or h to verify that the formula will still calculate correctly, or you can even apply the functions as methods to other objects for the same results.

Methods do exist in Flash without us creating them. As a matter of fact, it is difficult to write ActionScript without using methods. They are too numerous to have a full list here, because nearly each object has its own set of methods. You can review Chapter 8, "Welcome to ActionScript 2.0," for more on methods.

Here is a list of a few of the methods that are built in to Flash:

 myArray.push(); //adds an element to an array myMovie.stop(); //stops a movie clip's playhead myXML.load();   //loads an XML document into an XML object Math.sqrt();   //returns the square root of a number 

Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319 © 2008-2017.
If you may any questions please contact us: