Js deep learning-differences between js prototype constructor attributes-jsconstructor

Source: Internet
Author: User

Js deep learning-differences between js prototype constructor attributes-jsconstructor

In many js plug-ins, these two attributes are frequently used. I also used them when I write plug-ins myself. I know how to use them, but I don't know the specific differences. I have studied them today,

Constructor returns the constructor of an object (type instance). attributes and methods added through prototype are not returned.

Prototype returns a Type prototype that does not support post-dinner constructor.

Example:

<Html> 

In the Google Chrome console, enter myCat. constructor, and the following is displayed:

function (name,sex){      this.name=name;      this.sex=sex;      this.print=function(){        console.log("name:"+name+"  \r\n sex:"+sex);       }     } 
Enter cat. prototype to display the following information:

Object {color: "red", printColor: function}color: "red"constructor: function (name,sex){printColor: function (){arguments: nullcaller: nulllength: 0name: ""prototype: Object__proto__: function Empty() {}<function scope>__proto__: Object

As follows:








Object constructor and Prototype in JS.

This starts with the relationship between objects, instances, and prototypes,
If you have learned the object-oriented language or you know the object-oriented language, you can create an instance for an object. This is the premise. Now let's focus on it.
First, objects in js (functions are also objects) can be accessed to prototype ),
Then, the prototype you get through the object also has a pointer to the object (that is, constructor)
For example:
Function Person (){};
Var pro = Person. prototype; // obtain the object prototype.
Var obj = pro. constructor; // you can obtain the Person object.

The instance of the object created by the object is not mentioned much.
Var p = new Person (); // create an object instance.

The object instance also contains an implicit pointer to prototype (you can use '_ proto _' in browsers such as safrai, firefox, chrome, and opera to access it)
For example:
Var p2 = new Person ();
Var proro = p2. _ proto _; // you can obtain the reference of the object prototype,
Var bool = p2. _ proto _ = Person. prototype;
Alert (bool); // the prototype of the true object and the prototype obtained by the instance are the same object,

Summary:
Both objects and instances can access the prototype.
Object: Person. prototype;
Instance: p. _ proto __;

The prototype can access objects.
Person. prototype. constructor;
The instance can also access the object.
P. _ proto _. constructor;

What is the use of the constructor attribute in the js prototype?

Constructor is a built-in attribute.
Generated during instantiation
Overwrite the prototype attributes.

Output this. constructor in the function is clear.

The properties defined by this will not render the prototype.

During initialization, the prototype is obtained first and then this

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.