• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL



When a baby is born, we expect to see specific characteristics: a head, two eyes, two ears, two legs, and so forth. These are characteristics we expect to see in all babies because they’re hard-wired into our genetic code. This same type of logic applies to objects created with ActionScript. While names and ages vary, as they do when creating instances of our Person class, certain characteristics are universal to all instances. Let’s make another change to our Person class definition in accordance with this logic.

Open class1.fla in the Lesson06/Assets folder. With the Actions panel open, select Frame1. Update the lines of script that make up the Person class definition with the following:

							_global.Person = function(name, age){
							this.name = name;
							this.age = age;
							Person.prototype.legs = 2;
							Person.prototype.head = new Object();
							Person.prototype.head.eyes = 2;
							Person.prototype.head.memories = new Array();

Here, you can see we’ve moved some of the properties that were formally part of the class definition and attached them to the Person class’ prototype object—a special object associated with a class. This object—which is automatically created in the background whenever a new class is defined—contains properties and values that are universal to the class. You can think of this object as sitting right next to the class definition, as a kind of special attachment.


All classes of objects, including those built into ActionScript, have an associated prototype object. We will be tinkering with the prototype objects of these classes later in the lesson.

Nothing of value exists in a custom class’ prototype object until we script it— which is why we’ve added three properties to this object (and assigned their values) immediately after the class definition (as shown above). These prototype object properties are automatically inherited by all instances of that class, and their values are accessible in the same manner as described earlier. It’s important to understand how this prototype object works, so let’s look briefly at that.

When a new instance of the Person class is created, the class definition will add two properties to that instance, representing name and age . The following shows what happens when the value of one of those properties is accessed:

							myVariable = person1.age;

First, ActionScript looks at person1 and determines whether an age property is attached directly to that instance. When it sees that there is (the property was attached when the instance was passed through the class definition), it assigns the value of that property to myVariable . Now, let’s say that line of script looks like this instead:

							myVariable = person1.legs;

You’ll remember that the Person class definition doesn’t include a legs property; however, myVariable will still be given a value of 2. The reason for this is that in this scenario, ActionScript still looks at the instance first to determine whether the legs property is attached. If not, ActionScript says, “Well, person1 is an instance of the Person class. Since I can’t find the legs property on the instance, I’ll look at the Person.prototype object to see if it exists there.” At this point, ActionScript will see that Person.prototype.legs has been given a value of 2, and that person1.legs thus has a value of 2 as well. ActionScript will subsequently assign that value to myVariable .

ActionScript will always look for the property at the instance level first (we’ll discuss the term instance level in a moment). If it finds one, it quits looking; if it doesn’t, it looks at the prototype object of the instance’s class. Think of a class’ prototype object as a mirror. Any properties attached to it will be reflected in each instance of that class. Even though the instance doesn’t actually contain the property, it appears that it does to ActionScript.

You may be may be thinking that these properties worked just fine as part of the class definition (as in the previous exercise): Why would we want to move them to this prototype object? The reason to do so is that the prototype object enables class-wide enhancements—that is, changes to all instances of a particular class. If you change a property value on a class’ prototype object, all newly created or existing instances of that class automatically inherit that new value (with one exception, which we’ll discuss in a moment). To help you understand this concept a bit better, let’s do some testing.

Add the following lines of script at the end of the current script:

							person1 = new Person ("Justin", 16);
							person2 = new Person ("Derek", 29);
							trace (person1.legs);
							trace (person2.legs);

The first two lines create two instances of the Person object. The trace actions will display the legs property value of each instance in the Output window.

Choose Control > Test Movie to test the project up to this point:

The Output window will open and display the following:


You can see that both person1 and person2 share the same value for their legs property. However, we can make a class-wide change (that is, change the value for all instances) by adjusting the value of the legs property on the Person.prototype object. Let’s do that.

Close the test movie to return to the authoring environment. With the Actions panel open, select Frame 1 and update the value of Person.prototype.legs from 2 to 5. Then choose Control > Test movie to test the results again.

The Output window will open and display the following:


You can see that by simply changing a single value on the Person.prototype object, both person1 and person2 both get new values for their legs property. If you had 100 instances, each would reflect this new value. Adding, deleting, or updating properties on a class’ prototype object causes those changes to be immediately reflected in all instances of that class (with an exception, which we’ll discuss in the next section).

So why do we need to be able to set property values in both the class definition and the prototype object? The reason for this is that most object instances have both unique properties and universal properties (ones that they share with instances of the same class). For example, we all have different names and ages (properties you would likely set in the class definition), but we also have certain shared characteristics, such as two legs, a head, and memories (properties you would likely set using the prototype object).

Close the test movie to return to the authoring environment. With the Actions panel open, select Frame 1 and change the value of Person.prototype.legs back to 2. Save your work as class2.fla.

We’ll continue to use this file in the following exercise.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint