the limit in an effort to create other ways of implementing inheritance. This section examines some of the
alternatives to the standard ECMAScript inheritance paradigms.
Prototype chaining essentially copies all methods from an object to a class’s
what if there were a different way to accomplish this? There is. Using the zInherit library (available at
), it’s possible to accomplish method inheritance without
using prototype chaining. This small library supports all modern browsers (Mozilla, IE, Opera, Safari)
as well as some older browsers (Netscape 4.x, IE/Mac).
In order to use the zInherit library, you must include zinherit.js using the
tag. Chapter 5,
The zInherit library adds two methods to the
you may have guessed, the
method does the heavy lifting, copying the methods from
a given class. The following line uses prototype chaining to inherit methods from
ClassB.prototype = new ClassA();
This line can be replaced with the following:
method accepts one argument, which is the class from which to copy the methods.
Note that, as opposed to prototype chaining, this paradigm doesn’t actually create a new instance of the
class to inherit from, making it a little safer and freeing the developer from worrying about the construc-
method is a replacement for the
operator. Because this paradigm
doesn’t use prototype chaining at all, this line of code won’t work:
ClassB instanceof ClassA
method makes up for this loss, working with
to keep track of all
The entire polygon example can be rewritten using the zInherit library by replacing just two lines
method call must be used exactly where the prototype assign-
ment normally occurs in order to ensure proper inheritance.
07_579088 ch04.qxd 3/28/05 11:36 AM Page 116