Classes and constructors
Calling the constructor with new creates a new object, and the prototype property of the constructor is used as a prototype for the new object.
//constructor to initialize the newly created scope object
No object was created and returnedfunctionRange (from, to) { This. from =From ; This. to =to ;} Range.prototype={includes:function(x) {return This. from <= x && x <= This. To;}, foreach:function(f) { for(varx = Math.ceil ( This. from); X <= This. to; X + +) f (x); }, ToString:function() {return"(" + This. from + "..." + This. to + ")"}};varR =NewRange (1,3); R.include (2);//trueR.foreach (alert); Console.log (r.tostring ()); //(1...3)
The Range () constructor Simply initializes this and does not have to return the newly created object, and the constructor automatically creates the object.
Constructor property
The function has the prototype property, and the value of this property is an object that contains a unique non-enumerable property, constructor. The value of the constructor property is a function object
var function () {}; var p = f.prototype; // F-associated prototype object var c = p.constructor; // object associated with the prototype object alert (c = = = F); // true F.prototype.constructor = = F
The constructor properties that objects typically inherit refer to their constructors.
var New = = = F //true
The JavaScript authoritative guide constructor