The JavaScript authoritative guide constructor

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.