JavaScript learning 3: Prototype and inheritance, javascript prototype
Prototype
Each function we create has a prototype attribute, which is an object and its purpose is to include attributes and methods that can be shared by all instances of a specific type. Logically, prototype is the prototype of the object created by calling the constructor.
Why should we introduce the concept of prototype? The purpose of using the prototype is to allow all object instances to share its attributes and methods. In other words, you can directly add the object information to the prototype instead of defining the object information in the constructor.
For more information, see the code example:
<Span style = "font-size: 18px;"> // The prototype instance function Person () {}// declares a constructor Person. prototype. name = 'liany'; // Add the property Person in the prototype. prototype. age = 100; Person. prototype. run = function () {// Add the return this method to the prototype. name + this. age + 'struggling ...... ';}; Var person1 = new Person (); var person2 = new Person (); alert (person1.run = person2.run); // return true, the reference addresses of the methods are the same, that is, two objects share a method. </span>
To better understand the differences between the declaration method of constructor and the declaration method of the prototype mode, I have shared two diagrams with you to help you understand:
We can see that there are two more attributes in the prototype schema Declaration, both of which are automatically generated when an object is created. The _ proto _ attribute is a pointer to the prototype object of the instance. It is used to point to the constructor of the constructor. With these two attributes, you can access the kinship and method in the prototype.
You may find it strange to see that there is nothing in the constructor body in the above Code instance to access the values in the prototype object. What if the function body contains attributes or methods? A prototype execution process is involved here: You need to first find the attributes and methods in the constructor instance. If so, immediately return the result. If not, find it in its prototype object. If yes, return.
The prototype mode also has its disadvantage in object creation, that is, it ignores the process of passing parameters in the constructor initialization. The disadvantage is that the initialization values are the same, but this is precisely its biggest advantage, that is, sharing.
Inheritance
Inheritance is a core concept in object-oriented systems. In more orthodox object-oriented languages, inheritance is implemented in two ways: interface implementation and class inheritance. Our JavaScript only supports inheritance, but does not support interface implementation. Here we will introduce the concept of prototype chain. What is a prototype chain, we can see a piece of code.
<Span style = "font-size: 18px;"> // inherit instance function A () {this. name = 'liany';} function B () {this. age = 100;} function C () {this. address = 'China';} B. prototype. age = 200; B. prototype = new A (); // B inherits AC. prototype = new B (); // C inherits Bvar c = new C (); alert (c. name + ''+ c. age); // C has the attributes of A and B </span>
In JavaScript, the inherited function is called a super-type (that is, the parent class or base class in Object-Oriented), and the inherited function is called a child type (that is, a subclass or a derived class ). Inheritance is good, but it also has its own problems. For example, a literal rewriting prototype will interrupt the relationship, use a prototype of the reference type, and the child type cannot pass parameters to the super type.
In general, we use the prototype chain and the constructor to generate a combination inheritance.
<Span style = "font-size: 18px;"> // The combination inherits function Box (age) {this. name = 'lil'; this. age = age;} Box. prototype. run = function () {return this. name + this. age ;}; function Desk (age) {Box. call (this, age); // impersonate an object and pass the parameter to the super-type.} Desk. prototype = new Box (); // original form chain inherits var desk = new Desk (100); alert (desk. run (); // apparently, Desk inherits the Box run method. </span>
Combination inheritance is the most common inheritance method in JavaScript. However, there is also a problem with combination inheritance, that is, the super type will be called twice during use, one is when creating a child type, one is within the child Type constructor. This will bring performance overhead. How can this problem be solved? Let readers think ......