As mentioned in in the previous section, when attempting to access a property value as when assigning myVariable the value of person1.legs (myVariable = person1.legs; ) ActionScript will always look for the specified property at an instance level first. Only after it's looked there will ActionScript check the instance's prototype object. An instance-level property is a property that's set in the class definition when an instance is created, or it's a value set by directly addressing the instance. The person1 instance we created in the previous exercise has the following properties:
name = "Justin" // instance level age = 16 // instance level legs = 2 // prototype level head = new Object() // prototype level head.eyes = 2 // prototype level head.memories = new Array() // prototype level
Why is it important to distinguish between instance- and prototype-level properties? When an instance-level property and a prototype level property share the same name, the instance-level property value overrides the prototype property value (though only as it relates to that instance).
For example, the person1 instance currently gets its legs property from the Person.prototype object because it wasn't assigned that property at its creation. However, by setting the property value by addressing the instance itself for example, person1.legs = 1; we create an instance-level leg property for person1 , whose value overrides the prototype value used up to this point. All other instances will still have two legs except for person1 , which now only has one. The prototype value still exists for this instance, it's just hidden by the instance-level property with the same name. This could be likened to placing your left hand in front of your right hand. Your left hand (instance level) is all you can see; however, your right hand (prototype level) is still there it's just hidden from your view.
We can delete the instance-level leg property by using the following:
As a result, person1 will once again start reflecting the leg property (and its value) as set by the Person class prototype object.
Another important aspect of instance-level properties is that they allow instances to have properties in addition to those set in the class definition or inherited by the Prototype. To add such properties, you simply address the instance itself. For example:
person2.favoriteFood = "Pizza";
At this point, person2 is the only instance that has a favoriteFood property. We could easily add this property to other instances (either by assigning it directly or adding it to the prototype object), but until we do so, person2 is the only instance that includes the favoriteFood property.
If we added the property to the class definition, only new instances created from that point forward would be given the property, not existing instances. The reason for this is that the constructor function (class definition) only attaches properties to instances during their creation.
Instance-level properties allow you to individualize instances without sacrificing the benefits of object classes.