O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Constructor Parameters and Arguments

A constructor parameter is special type of local variable that is created as part of a constructor-method definition. Unlike regular local variables, a constructor parameter's initial value can be (or in some cases, must be) supplied externally when a new object is instantiated.

Constructor parameters are not created with the keyword var. Instead, to create a constructor parameter, we simply provide the desired name and variable initializer within the parentheses of a constructor function definition, as shown in the following generalized code:

class SomeClass {
  function SomeClass (identifier = value) {
  }
}

In the preceding code, identifier is the name of a constructor parameter, and value is the parameter's initial value.

To create more than one parameter for a constructor method, we list multiple parameter names, separated by commas, as shown in the following generalized code (notice the line breaks, which are both legal and common):

class SomeClass {
  function SomeClass (identifier1 = value1,
                      identifier2 = value2,
                      identifier3 = value3) {
  }
}

By default, the initial value of a constructor parameter is set to the value supplied in that parameter's definition. However, a constructor parameter's value can alternatively be supplied when an object is instantiated, using the following generalized object-creation code:

new SomeClass(value1, value2, value3)

In the preceding code, value1, value2, and value3 are values that are assigned, in order, to the constructor parameters of SomeClass's constructor method. A value supplied to a constructor parameter when an object is instantiated (as shown in the preceding code) is known as a constructor argument. Using a constructor argument to supply the value of a constructor parameter is known as passing that value to the constructor.

When a constructor parameter definition does not include a variable initializer, that parameter's initial value must be supplied via a constructor argument. Such a parameter is known as a required constructor parameter. The following generalized code shows how to create a class with a single required constructor parameter (notice that the parameter definition does not include a variable initializer):

class SomeClass {
  function SomeClass (requiredParameter) {
  }
}

Any code that creates an instance of the preceding class must supply requiredParameter's value using a constructor argument, as shown in the following generalized code:

new SomeClass(value)

Failure to supply a constructor argument for a required parameter causes an error either when the program is compiled (if the program is compiled in strict mode) or when the program runs (if the program is compiled in standard mode). We'll learn the difference between strict mode and standard mode compilation in Chapter 7.

Tip

When creating a new object without constructor arguments, some programmers choose to retain the constructor-argument parentheses. For example, some programmers prefer to write:

new VirtualPet()

rather than:

new VirtualPet

The choice is entirely stylistic; ActionScript allows both formats. However, the ActionScript programming community favors the former style (with parentheses) over the latter (without parentheses). Hence, from now on, this book will always include parentheses when creating new objects, even when no constructor arguments are used.

Using the preceding generalized parameter code is a guide, let's add a new constructor method to our VirtualPet class, and define a single, required constructor parameter, name. We'll use the value of the name parameter to set each VirtualPet object's petName instance variable. Here's the basic code for the constructor method, shown, for the moment, without any code in the constructor body:

package zoo {
  internal class VirtualPet {
    internal var petName = "Unnamed Pet";

    public function VirtualPet (name) {
    }
  }
}

Because name is a required parameter, its initial value must be supplied externally at object-creation time. Accordingly, we must update the code that creates our VirtualPet object in the VirtualZoo constructor. Previously, the code looked like this:

package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
      var pet = new VirtualPet;
      pet.petName = "Stan";
    }
  }
}

Here's the updated version, which passes the value "Stan" to the VirtualPet constructor instead of assigning it to the new instance's petName variable:

package zoo {
  public class VirtualZoo {
    public function VirtualZoo () {
      var pet = new VirtualPet("Stan");
    }
  }
}

When the preceding code creates the VirtualPet instance, VirtualPet's constructor runs, and the constructor argument "Stan" is assigned to the name parameter. Hence, within the VirtualPet constructor, we can use the name parameter to assign the value "Stan" to the new VirtualPet object's petName instance variable. To do that, we need to specify petName's value using an identifier expression. The next section describes expressions and identifier expressions.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required