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

9.6.7 Constructor Overloading and Factory Methods

9.6.7 Constructor Overloading and Factory Methods

Sometimes we want to allow objects to be initialized in more than one way. We might want to create a Complex object initialized with a radius and an angle (polar coordinates) instead of real and imaginary components, for example, or we might want to create a Set whose members are the elements of an array rather than the arguments passed to the constructor.

One way to do this is to overload the constructor and have it perform different kinds of initialization depending on the arguments it is passed. Here is an overloaded version of the Set constructor, for example:

function Set() {

this.values = {}; // The properties of this object hold the set

this.n = 0; // How many values are in the set

// If passed a single array-like object, add its elements to the set

// Otherwise, add all arguments to the set

if (arguments.length == 1 && isArrayLike(arguments[0]))

this.add.apply(this, arguments[0]);

else if (arguments.length > 0)

this.add.apply(this, arguments);


Defining the Set() constructor this way allows us to explicitly list set members in the constructor call or to pass an array of members to the constructor. The constructor has an unfortunate ambiguity, however: we cannot use it to create a set that has an array as its sole member. (To do that, we’d have to create an empty set and then call the add() method explicitly.)

In the case of complex numbers initialized to polar coordinates, constructor overloading really isn’t viable. Both representations of complex numbers involve two floating-point numbers and, unless we add a third argument to the constructor, there is no way for the constructor to examine its arguments and determine which representation is desired. Instead, we can write a factory method—a class method that returns an instance of the class. Here is a factory method for returning a Complex object initialized using polar coordinates:

Complex.polar = function(r, theta) {

return new Complex(r*Math.cos(theta), r*Math.sin(theta));


And here is a factory method for initializing a Set from an array:

Set.fromArray = function(a) {

s = new Set(); // Create a new empty set

s.add.apply(s, a); // Pass elements of array a to the add method

9.6 Object-Oriented Techniques in JavaScript | 227

return s; // Return the new set };

The appeal of factory methods here is that you can give them whatever name you want, and methods with different names can perform different kinds of initializations. Since constructors serve as the public identity of a class, however, there is usually only a single constructor per class. This is not a hard-and-fast rule, however. In JavaScript it is possible to define multiple constructor functions that share a single prototype object, and if you do this, objects created by any of the constructors will be of the same type. This technique is not recommended, but here is an auxiliary constructor of this type:

// An auxiliary constructor for the Set class.

function SetFromArray(a) {

// Initialize new object by invoking Set() as a function,

// passing the elements of a as individual arguments.

Set.apply(this, a);


// Set the prototype so that SetFromArray creates instances of Set

SetFromArray.prototype = Set.prototype;

var s = new SetFromArray([1,2,3]); s instanceof Set // => true

In ECMAScript 5, the bind() method of functions has special behavior that allows it to create this kind of auxiliary constructor. See §8.7.4 .

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