Understanding JavaScript's prototype chain

Source: Internet
Author: User

To understand the prototype chain, first understand the following points clearly:

1. All functions are instantiated with the function, which contains the prototype attribute, which is the prototype object.

2. All objects have the __proto__ property, which points to the prototype prototype object of the object constructor.

The constructor property of the 3.prototype prototype object points to the constructor in which it resides, that is, the constructor itself.

4.prototype is for the function, __proto__ is about the object.

5. The function itself is also an object.

Recognizing the above, let's look at the prototype chain first:

 function   SetName () { this . Name = ' Mark ' ;  function   Setage () { this . Age = 20;    } setage.prototype  = new   SetName ();  var  obj = new      Setage (); Console.log (obj.name,obj.age);  // mark   Console.log (obj.constructor  = = = SetName); // true 

The above code we use the constructor Setage () to instantiate an object obj, where age is written by the constructor, and the Name property is written by a Setage prototype object SetName. Obj.constructor = = = SetName Returns True because, in the constructor of the Obj object, there is no direct consructor, its constructor property is inherited by the __proto__ property, and __proto__ The prototype object that points to the constructor of obj, which is Setage.prototype, which is SetName, returns True.

Then, the simplest prototype chain should be, the instance object "= = Constructor" = = Constructor Prototype Object "= = Object" = = object.prototype "= = NULL, while the constructor in the prototype object (constructor property) is equal to the constructor in which it resides.

function Createo () {        this. x = 1;    }      var New Createo ();    Console.log (o.__proto__); // Createo.prototype    Console.log (o.__proto__.__proto__); // Object    Console.log (o.__proto__.__proto__.__proto__); // NULL

So plainly, the essence of the prototype chain is the inheritance chain formed between the instantiated object and the prototype object (prototype).

Again, inheritance:

The most common way to inherit from a prototype chain is:

Copy Code1functionPerson (name) {2 This. Name =name;3 } 4 Person.prototype.showName =function(){ 5 Console.log ( This. Name); 6 } 7functionStudent (name,age) {8 Person.call ( This, name); 9 This. Age =Age ;10 }One student.prototype =NewPerson ();Student.prototype.contructor =Student;Student.prototype.showAge =function(){Console.log ( This. age);15 }16varStu =NewStudent (' Zhang San ', 12);17stu.showname ();Stu.showage ();

Reference: https://www.cnblogs.com/DF-fzh/p/5619319.html

https://www.cnblogs.com/Yirannnnnn/p/4896542.html#undefined

Understanding JavaScript's prototype chain

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.