JavaScript: The Definitive Guide, Sixth Editio javaScript权威指南(第6版) pdf 文字版-文字版, javascript电子书, 和javascript 有关的电子书:

9.7.1 Defining a Subclass

9.7.1 Defining a Subclass

JavaScript objects inherit properties (usually methods) from the prototype object of their class. If an object O is an instance of a class B and B is a subclass of A, then O must also inherit properties from A. We arrange this by ensuring that the prototype object of B inherits from the prototype object of A. Using our inherit() function ( Example 6-1 ), we write:

B.prototype = inherit(A.prototype); // Subclass inherits from superclass B.prototype.constructor = B; // Override the inherited constructor prop.

These two lines of code are the key to creating subclasses in JavaScript. Without them, the prototype object will be an ordinary object—an object that inherits from Object.prototype—and this means that your class will be a subclass of Object like all classes are. If we add these two lines to the defineClass() function (from §9.3 ), we can transform it into the defineSubclass() function and the Function.proto type.extend() method shown in Example 9-11 .

Example 9-11. Subclass definition utilities

// A simple function for creating simple subclasses

function defineSubclass(superclass, // Constructor of the superclass constructor, // The constructor for the new subclass methods, // Instance methods: copied to prototype statics) // Class properties: copied to constructor

{ // Set up the prototype object of the subclass constructor.prototype = inherit(superclass.prototype); constructor.prototype.constructor = constructor; // Copy the methods and statics as we would for a regular class if (methods) extend(constructor.prototype, methods); if (statics) extend(constructor, statics); // Return the class return constructor;


// We can also do this as a method of the superclass constructor Function.prototype.extend = function(constructor, methods, statics) { return defineSubclass(this, constructor, methods, statics); };

Example 9-12 demonstrates how to write a subclass “manually” without using the defineSubclass() function. It defines a SingletonSet subclass of Set. A SingletonSet is a specialized set that is read-only and has a single constant member.

9.7 Subclasses | 229

Example 9-12. SingletonSet: a simple set subclass

// The constructor function function SingletonSet(member) {

this.member = member; // Remember the single member of the set }

// Create a prototype object that inherits from the prototype of Set. SingletonSet.prototype = inherit(Set.prototype);

// Now add properties to the prototype. // These properties override the properties of the same name from Set.prototype. extend(SingletonSet.prototype, {

// Set the constructor property appropriately

constructor: SingletonSet,

// This set is read-only: add() and remove() throw errors

add: function() { throw "read-only set"; },

remove: function() { throw "read-only set"; },

// A SingletonSet always has size 1

size: function() { return 1; },

// Just invoke the function once, passing the single member.

foreach: function(f, context) {, this.member); },

// The contains() method is simple: true only for one value

contains: function(x) { return x === this.member; }


Our SingletonSet class has a very simple implementation that consists of five simple method definitions. It implements these five core Set methods, but inherits methods such as toString(), toArray() and equals() from its superclass. This inheritance of methods is the reason for defining subclasses. The equals() method of the Set class (defined in §9.6.4 ), for example, works to compare any Set instance that has working size() and foreach() methods with any Set that has working size() and contains() methods. Because SingletonSet is a subclass of Set, it inherits this equals() implementation automatically and doesn’t have to write its own. Of course, given the radically simple nature of singleton sets, it might be more efficient for SingletonSet to define its own version of equals():

SingletonSet.prototype.equals = function(that) { return that instanceof Set && that.size()==1 && that.contains(this.member); };

Note that SingletonSet does not statically borrow a list of methods from Set: it dynamically inherits the methods of the Set class. If we add a new method to Set.prototype, it immediately becomes available to all instances of Set and of SingletonSet (assuming SingletonSet does not already define a method by the same name).

友情链接It题库(| 版权归yishouce.com所有| 友链等可联系|粤ICP备16001685号-1