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

9.2.2 The constructor Property

The instanceof operator does not actually check whether r was initialized by the Range constructor. It checks whether it inherits from Range.prototype. Nevertheless, the instanceofsyntax reinforces the use of constructors as the public identity of a class. We’ll see the instanceof operator again later in this chapter.

9.2.2 The constructor Property

In Example 9-2 we set Range.prototype to a new object that contained the methods for our class. Although it was convenient to express those methods as properties of a single object literal, it was not actually necessary to create a new object. Any JavaScript function can be used as a constructor, and constructor invocations need a prototype property. Therefore, every JavaScript function (except functions returned by the EC-MAScript 5 Function.bind()method) automatically has a prototype property. The value of this property is an object that has a single nonenumerable constructor property. The value of the constructor property is the function object:

var F = function() {}; // This is a function object. var p = F.prototype; // This is the prototype object associated with it. var c = p.constructor; // This is the function associated with the prototype. c === F // => true: F.prototype.constructor==F for any function

9.2 Classes and Constructors | 203

The existence of this predefined prototype object with its constructor property means that objects typically inherit a constructor property that refers to their constructor. Since constructors serve as the public identity of a class, this constructor property gives the class of an object:

var o = new F(); // Create an object o of class F o.constructor === F // => true: the constructor property specifies the class

Figure 9-1 illustrates this relationship between the constructor function, its prototype object, the back reference from the prototype to the constructor, and the instances created with the constructor.

Notice that Figure 9-1 uses our Range() constructor as an example. In fact, however, the Range class defined in Example 9-2 overwrites the predefined Range.prototype object with an object of its own. And the new prototype object it defines does not have a constructor property. So instances of the Range class, as defined, do not have a con structor property. We can remedy this problem by explicitly adding a constructor to the prototype:

Range.prototype = { constructor: Range, // Explicitly set the constructor back-reference includes: function(x) { return this.from <= x && x <=; }, foreach: function(f) {

for(var x = Math.ceil(this.from); x <=; x++) f(x); }, toString: function() { return "(" + this.from + "..." + + ")"; }


Another common technique is to use the predefined prototype object with its constructor property, and add methods to it one at a time:

// Extend the predefined Range.prototype object so we don't overwrite // the automatically created Range.prototype.constructor property. Range.prototype.includes = function(x) { return this.from<=x && x<; }; Range.prototype.foreach = function(f) {

for(var x = Math.ceil(this.from); x <=; x++) f(x); }; Range.prototype.toString = function() {

return "(" + this.from + "..." + + ")"; };

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