Inheritance using Closures and Prototypes
There are two significant techniques for implementing the OOP concept of inheritance with JavaScript code. The first technique uses closures, and the other technique makes use of a feature of the language named prototyping. Early implementations of the Microsoft AJAX library made use of closures-based inheritance, and in the final stage the code was rewritten to use prototypes. In the following few pages we'll quickly discuss both techniques.
Inheritance Using Closures
In classic OOP languages such as C#, C++, or Java, you can extend classes through inheritance. Closure-based inheritance is implemented by creating a member in the derived class that references the base class, and calling that member. This causes the derived class to inherit all the base class members, effectively implementing the concept of inheritance.
To demonstrate this technique, we'll implement two classes: Car and SuperCar. The Car class constructor receives a car name as parameter, and it has a method named Drive(). The class SuperCar inherits the functionality of Car, and adds a new method named Fly(), reflecting the additional functionality it has in addition to what Car has to offer. The diagram in Figure 3-6 describes these two classes.

Figure 3-6. Car and SuperCar class diagram
Remember that in JavaScript the implementation of a class diagram can be achieved in multiple ways. The code reflects the concept of the diagram, but not also the implementation details, as the C# code would. Here's a possible implementation of Car and SuperCar:
<script type="text/javascript"> // to be used as the Drive method of Car function Drive() { document.write("My name is " + this.Name + " and I'm driving. "); } // class Car function Car(name) { // create the Name property this.Name = name; // Car knows how to drive this.Drive = Drive; } // to be used as the Fly method of SuperCar this.Fly = function() { document.write("My name is " + this.Name + " and I'm flying! "); } // class SuperCar function SuperCar(name) { // implement closure inheritance this.inheritsFrom = Car; this.inheritsFrom(name); // SuperCar knows how to fly this.Fly = Fly; } // create a new Car and then Drive var myCar = new Car("Car"); myCar.Drive(); // create SuperCar object var mySuperCar = new SuperCar("SuperCar"); // SuperCar knows how to drive mySuperCar.Drive(); // SuperCar knows how to fly mySuperCar.Fly(); </script>
%3Cscript%20type%3D%22text%2Fjavascript%22%3E%20%0A%0D%0A%2F%2F%20to%20be%20used%20as%20the%20Drive%20method%20of%20Car%20%0A%0D%0Afunction%20Drive%28%29%20%0A%0D%0A%7B%20%0A%0D%0Adocument.write%28%22My%20name%20is%20%22%20%2B%20this.Name%20%2B%20%0A%0D%0A%22%20and%20I%27m%20driving.%20%0A%22%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20class%20Car%20%0A%0D%0Afunction%20Car%28name%29%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20create%20the%20Name%20property%20%0A%0D%0Athis.Name%20%3D%20name%3B%20%0A%0D%0A%2F%2F%20Car%20knows%20how%20to%20drive%20%0A%0D%0Athis.Drive%20%3D%20Drive%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20to%20be%20used%20as%20the%20Fly%20method%20of%20SuperCar%20%0A%0D%0Athis.Fly%20%3D%20function%28%29%20%0A%0D%0A%7B%20%0A%0D%0Adocument.write%28%22My%20name%20is%20%22%20%2B%20this.Name%20%2B%20%22%20and%20I%27m%20flying%21%20%0A%22%29%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20class%20SuperCar%20%0A%0D%0Afunction%20SuperCar%28name%29%20%0A%0D%0A%7B%20%0A%0D%0A%2F%2F%20implement%20closure%20inheritance%20%0A%0D%0Athis.inheritsFrom%20%3D%20Car%3B%20%0A%0D%0Athis.inheritsFrom%28name%29%3B%20%0A%0D%0A%2F%2F%20SuperCar%20knows%20how%20to%20fly%20%0A%0D%0Athis.Fly%20%3D%20Fly%3B%20%0A%0D%0A%7D%20%0A%0D%0A%2F%2F%20create%20a%20new%20Car%20and%20then%20Drive%20%0A%0D%0Avar%20myCar%20%3D%20new%20Car%28%22Car%22%29%3B%20%0A%0D%0AmyCar.Drive%28%29%3B%20%0A%0D%0A%2F%2F%20create%20SuperCar%20object%20%0A%0D%0Avar%20mySuperCar%20%3D%20new%20SuperCar%28%22SuperCar%22%29%3B%20%0A%0D%0A%2F%2F%20SuperCar%20knows%20how%20to%20drive%20%0A%0D%0AmySuperCar.Drive%28%29%3B%20%0A%0D%0A%2F%2F%20SuperCar%20knows%20how%20to%20fly%20%0A%0D%0AmySuperCar.Fly%28%29%3B%20%0A%0D%0A%3C%2Fscript%3E
Loading this script in a browser would generate the results shown in Figure 3-7. It can be tested online at http://www.cristiandarie.ro/asp-ajax/JavaScriptClosureInheritance.html.

Figure 3-7. JavaScript Inheritance
The exercise demonstrates that inheritance really works. SuperCar only defines the capability to Fly(), yet it can Drive() as well. The capability to Drive() and the Name property are inherited from Car.
At the first sight the code can look a bit complicated, especially if you're a C# veteran. The Drive() and Fly() functions aren't defi ned inside Car and SuperCar, as you'd do in a C# class. Instead, we stored these methods/functions in the global context, and referenced them in Car and SuperCar, to avoid the memory leaks that were discussed earlier in this chapter. You can, however, define Drive() inside Car, and Fly() inside SuperCar, without losing any functionality.
If you comment the execution of this.inheritsFrom(name) from SuperCar, it won't inherit the capabilities of Car any more. If you make this test in FireFox, you'll see the following eloquent error message in the Error Console window of Firefox:

Figure 3-8. Signs of failed inheritance
The problem with the presented inheritance solution is that it's not very elegant. Writing all functions and classes in the global context can quickly degenerate into chaos; and things get even more complicated if you want to have classes that have functions with the same name. Needless to say, this isn't something you need to be dealing with when writing your code. Luckily, JavaScript has a very neat feature that allows us implement inheritance in a much cleaner way: prototyping.
Trackback(0)
|