JavaScript Constructor Learning Notes

Source: Internet
Author: User

1 understanding JavaScript Constructor Implementation Principles

In JavaScript, each function is named "Prototype" and is used to refer to the prototype object. This prototype object is also known as the "constructor" property, which in turn references the function itself. This is a circular reference

2 JavaScript Quest: Constructor Constructor

In addition to creating objects, the constructor (constructor) also does another useful thing--automatically sets the prototype object (prototype objects) for the new object that was created. The prototype object is stored in the Constructorfunction.prototype property.

3 JavaScript inheritance in a detailed way

1) prototype chain inheritance

function Parent () {        this.name = ' Mike ';    }    function Child () {        this.age =;    }    Child.prototype = new parent ();//child inherits the parent, through the prototype, forms the chain    var test = new Child ();    alert (test.age);    alert (test.name);//Get inherited Property}

2) Combination Inheritance

   function Parent (age) {        this.name = [' Mike ', ' Jack ', ' Smith '];        This.age = age;    }    Parent.prototype.run = function () {        return this.name  + ' is both ' + this.age;    };    function Child (age) {        parent.call (this,age);//object impersonating, giving super type parameter    }    child.prototype = new Parent ();//prototype chain inheritance    var test = new Child (21);//write New Parent (21) also line    alert (Test.run ());//mike,jack,smith is both21

Use the prototype chain to implement inheritance of prototype properties and methods, and to implement inheritance of instance properties by borrowing constructors. This enables the reuse of functions both by defining methods on the prototype and ensuring that each instance has its own properties.

4 JavaScript Inheritance Detailed

A series of articles, not read

JavaScript Constructor Learning Notes

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.