Section 11.4. Chaining Constructors and JS Inheritance


11.4. Chaining Constructors and JS Inheritance

JavaScript is not a typical OO language, and shouldn't be pushed, pummeled, or constrained into one. It has its own strengths, which should be used to advantage. Still, there are pieces of traditional object-oriented design that would be nice to use in applications. In the last section we saw one type of OO-based design: encapsulation. This section covers another: inheritance.

Inheritance incorporates, or inherits, another object's methods and properties in a new object. It's the fundamental power of class-oriented development because one class can inherit from another class, choosing to override whatever functions that have a new behavior in the new class. Something similar can be used in JS to emulate this behavior, starting with JavaScript 1.3the function methods of apply and call.

Returning to previous examples, when a function defining a new object is written, it becomes the object constructor and is invoked when the new keyword is used with the function:

theobj = new DivObj(params);

Both the function apply and call methods allow you to apply or invoke a method within the context of another object. If used with an object constructor, it chains the constructors in such a way that all properties and methods of the one object are inherited by the containing object. The only difference between the two is the parameters passed; the behavior is the same. The call method takes the containing object as the first parameter, identified using this, and each of the arguments you want to pass to the constructor of the contained object:

obj.call(this,arg1,arg2,..., argn);

The apply method takes a reference to the containing object and the arguments array of the container. If the contained object has two parameters, and the container three, only the first two arguments of the arguments array are passed to the contained object:

obj.apply(this,arguments);

If you're sharing a set of arguments, use apply. Otherwise, use call.

Example 11-6 uses apply and chained constructors to demonstrate inheritance. The first object created, tune, stores information about a song's title and type. It also has a method that returns a string containing both. The second object, artist_tune, also contains a property for the artist, as well as a function to create a string of all properties. The apply method is called directly off of the tune function/object. In addition, once both objects are defined, the artist_tune prototype is assigned the tune constructor.

Example 11-6. Chained constructors and inheritance through the function method apply

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Inheritance</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> //<![CDATA[ function tune(title,type) {    this.title = title;    this.type = type;    this.getTitle=function(  ) {      return "Song: " + this.title + " Type: " + this.type;    } } function artist_tune(title,type,artist) {    this.artist = artist;    this.toString("Artist is " + artist);    tune.apply(this,arguments);    this.toString = function (  ) {      return "Artist: " + this.artist + " " + this.getTitle(  );    } } artist_tune.prototype = new tune(  ); var song = new artist_tune("I want to hold your hand", "rock", "Beatles"); alert(song.toString(  )); //]]> </script> </head> <body> </body> </html>

Handy little methods, call and apply. Sometimes, though, you don't need inheritance, or even a class, when creating custom objects. Sometimes all you need is one object.

This is all going to be a bit much if you've never worked with a programming language prior to this book. Or even if you have, because JavaScript has some pretty unusual concepts. Some of the functionality described in this chapter, such as chained constructors, is pretty rare, so don't worry if you find your eyes glazing over on that one. However, creating custom objects and the use of prototype are common, so you may want to go over the other sections a couple of times until you feel more comfortable. Experiment with the examples, and try out some of your own.





Learning JavaScript
Learning JavaScript, 2nd Edition
ISBN: 0596521871
EAN: 2147483647
Year: 2006
Pages: 151

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