|
|
||||||||
| Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0 | ||||||||
|
||||||||
|
|
ActionScript 2.0 |
|
||||||
02. A little bit of OOP in ActionScript 1.0Before we move on to ActionScript 2.0, let's review OOP in ActionScript 1.0. This section is especially useful for readers who are not familiar with OOP in Flash 5 and Flash MX. Experienced developers may skip ahead to What's new in ActionScript 2.0? Although ActionScript 1.0 is not a true OOP language, developers have been programming object-oriented applications with it for some time. Everything in AS1 depends on the prototype chain -- the underlying structure of how objects are related. As a result, OOP in ActionScript 1.0 requires a knowledge of this prototype chain (or more specifically, the prototype keyword). ActionScript 1.0 classes are implemented just like regular functions. Methods (and sometimes properties) are attached to the prototype object of the class. For example: // Wizard class
function Wizard() {
}
// A method called help() is attached to the prototypeWhen Flash looks for a property or method of an object and can't find it inside the object, it searches along the prototype chain of the object. If this help() method is placed inside the class instead, Flash creates a new copy of the method for every instance of the Wizard class: // This creates a new copy of the function help() in every instance
function Wizard() {
this.help = function() {
};
}
To Java, C++ or C# programmers, this syntax looks more familiar, with the
method inside the class. However, in order to save memory (by sharing the
same code at one location), methods (and shared properties) should be placed
in the prototype object of the class.
In the following example, if we have two methods of the same name, one attached to the prototype object and one inside the class, Flash will reach the internal method first (it overrides the prototype method): // AS1_OOP_01.fla
function TestClass() {
this.method = function() {
trace("Internal method");
};
// Attach a method to the prototype object of the class
TestClass.prototype.method = function() {
trace("Prototype method");
};
TestClass.prototype.prop = ">>> Prototype prop"; // Create an instance of the TestClass class var w = new TestClass(); // Internal method is located before the prototype method w.method(); // Replace the Internal method
w.method = function() {
trace("New method");
};
w.method();
// Delete the Internal method delete w.method; // The only method remaining is the prototype method w.method(); // Test the properties trace(w.prop); w.prop = ">>> New prop"; trace(w.prop); delete w.prop;
trace(w.prop);
The output of this example is shown below.
As you can see, OOP in ActionScript 1.0 can be rather confusing for beginners. Knowing where to put code is important because undesirable results may occur that can lead to hair lost debugging the code. As they say in commercials: But wait, there's more... One of the important concepts in OOP is Inheritance. Basically you get whatever your parents and ancestors have, for free. If you don't like whatever they pass on to you, you can change them to your liking. Inheritance in ActionScript 1.0 is officially coded like this: function SuperClass() {
// SuperClass constructor
}
SuperClass.prototype.parentMethod = function() {
};
// A subclass that inherits from SuperClass
function SubClass() {
super();
}
// Set up the inheritance relationship: SubClass inherits from SuperClassThere are a couple of issues with this set up: The syntax is non-standard:
How does over-writing the prototype object with an instance of the superclass
establish inheritance? No other OOP language works like this. In effect,
this is what SuperClass is saying to the SubClass: "If you want to
inherit from me, first create a new instance of me, and inherit from it,
don't inherit from me directly." What happens is, there are all these
nameless instances of superclasses hanging around, taking up memory, just
to define the parent/child relationships. The benefits of OOP are numerous (such as more maintainable, scalable and reusable code). If we can achieve the benefits without all that mess, it is worth every bit (and byte) of the effort! The ECMA-262 Edition 4 proposal addresses most of these issues, and ActionScript 2.0 takes advantage of this proposal by introducing a new OOP model and concepts.
|
||||||||
©2003 Ultrashock.com - All rights reserved |