Js deep learning-differences between js prototype constructor attributes

Source: Internet
Author: User

Js deep learning-differences between js prototype constructor attributes

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:

 <Script type = "text/javascript"> var cat = function (name, sex) {this. name = name; this. sex = sex; this. print = function () {console. log ("name:" + name + "\ r \ n sex:" + sex) ;}} cat. prototype. color = "red"; cat. prototype. printColor = function () {console. log ("color:" + this. color + "\ r \ n name" + this. name);} var myCat = new cat ("cavent", "boy"); </script>Js prototype attributes and constructor attributes

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() {}
 
  __proto__: Object
 

As follows:







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.