JS Object-oriented-prototype object, instance object, constructor relationship

Source: Internet
Author: User

JS in each of the creation of a function, the function will automatically have a prototype property, why that??

Because the object on the root has a prototype property, and all the objects in JS inherit from object, so all the objects in JS have a prototype attribute, and in JS the function is the object, So each function in JS also has a prototype attribute.

For example: function person () {...} and function Dog () {...}



And each prototype property gets a constructor property


The constructor property has an implied pointer to the function where the prototype property resides.


This shows that when the Person.prototype.constructor is popped with the alert function, the result is

Function person{....}

When an object instance is created through new person (), the instance contains an implied pointer to the Person.prototype


In conjunction with the previous example, the relationship between the constructor, the instance object, and the prototype object is illustrated by a diagram.


Each instance of person contains a stealth pointer to Person.prototype, in other words, they are not directly related to the constructor function.

You can use isPrototypeOf () to determine whether an instance points to a prototype object, or whether an instance originates from a prototype object.

Alert (Person.prototype.isPrototypeof (p1)) returns True, indicating that P1 points to Person.prototype,

Person.prototype is the prototype object of the instance P1.

JS Object-oriented-prototype object, instance object, constructor relationship

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.