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.
NOTE
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.