Prototyping an Object


Most of the time when you hear the word "prototype," it means a first working draft of a project. And in Flash, that can be true as well, but it can also mean the prototype chain.

The prototype chain in Flash starts with the Object object. All other objects are based on this supreme parent object. This means that if you add a property or method to the Object object, all other objects in Flash will then have the ability to use this property or method. But the Object object is already instantiated, so how do you add things to its constructor class after the fact? By using the prototype property.

The prototype property is designed to be able to add methods and properties to any and all object classes after the object class has already been instantiated. It's easy to use; you just call the object class's name, add the prototype property, and then add the new property or method name.

Here is an example of adding a method to the Array class that will, when called, send back a message saying hello:

 //add the method to the Array class Array.prototype.hello = function(){      return "Hello world!"; } //now create a new Array var myArray:Array = new Array(); //send the message to the output panel trace(myArray.hello()); //Output: Hello world! 

The preceding code adds a new method to the Array class that will send a message back using the return action. We then create a new array and use the trace action to send the message to the Output panel.

But now let's say we want all object classes to able to instantly know what kind of object they are. We could add a new method to the Object object like this:

 //add a method to the Object object Object.prototype.whatAmI = function(){      return typeof this; } //now create a new Array var myArray:Array = new Array() //now using the new Object method //trace what kind of object the array and the _root timeline are trace(this.whatAmI()); trace(myArray.whatAmI()); //Output: movieclip //       object 

The preceding code adds a method to the Object object. Then we create an array. After that, we call the new method on the array we created as well as the root timeline and send the results to the Output panel.

TIP

Even though you can add methods and properties to the core objects in Flash using the prototype property, you cannot do the same with your own classes because Flash's classes are precompiled and our custom class is not. But because it is your own class, you do not need to add methods or properties at runtime, you can just add them directly to the class file unlike the core classes of ActionScript.


Now that you have an understanding of how the object-oriented programming model works and the benefits it has, we can go over some of the differences in ActionScript 2.0 from ActionScript 1.0.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net