What is a prototype? Prototype is a concept that we did not mention in the basic learning of JS. Prototype is a general term that mainly includes Prototype object (prototype) , Object prototype (__proto__) , prototype chain Wait, these concepts are also commonly asked in interviews according to statistics. This article will help you understand and master the relevant knowledge of prototypes, so that you will no longer be confused.
We have studied many object-oriented languages, such as Java, C++, etc., but JavaScript is an exception. Before ES6, there was no concept of classes. So how did we create objects before? It turns out that before ES6, we used constructors to create instantiated objects. The constructor is a special function that contains the public characteristics of the object. It only makes sense to use it in conjunction with new .
<script> function Animal(name,age){ //The first letter of the constructor name is capitalized this.name=name; this.age=age; this.eat=function(){ console.log('I'm eating'); } } var dog=new Animal('Wangcai',3) //To be used together with new to create an object console.log(dog.name); console.log(dog.age); dog.eat() </script>
<script> function Animal(name,age){ this.name=name; this.age=age; } var dog=new Animal('Wangcai',3) console.log(dog.name); console.log(Animal.name); </script>
<script> function Animal(name,age){ this.name=name; this.age=age; } var dog=new Animal('Wangcai',3) Animal.color='black' console.log(Animal.color); console.log(dog.color); </script>
Before starting to explain what a prototype object is, let us first explain a case. It is still the Animal class just now. We created multiple instantiated objects and output the two methods of their instantiated objects. After comparison, we found that false was output, that is, the addresses of the complex data types of the two are different. What is the reason?
<script> function Animal(name,age){ this.name=name; this.age=age; this.eat=function(){ console.log('I'm eating'); } } var dog=new Animal('Wangcai',3) var cat=new Animal('Mimi',3) var pig=new Animal('humhem',3) var fish=new Animal('Gulu',3) var sheep=new Animal('咩咩',3) console.log(dog.eat==cat.eat); </script>
When we create an instantiated object, the new process will first create a new object, but complex data types will open up a space to store (objects, methods), which results in countless openings for the same method in the constructor. block memory, causing an extreme waste of memory
Constructor prototype prototype is an attribute within the constructor. Its attribute is a pointer pointing to an object. This object stores public methods. The methods stored in this object are then used to create instances through the constructor. This method can be used publicly when converting objects, and there is no need to open up multiple duplicate memory spaces for multiple identical complex data types. In order to solve the above-mentioned memory waste problem, it can also be directly called a prototype object.
Solution: We use the prototype object to store the public method, let the instantiated object call the method, and compare whether the addresses of the two are the same
<script> function Animal(name,age){ this.name=name; this.age=age; } Animal.prototype.eat=function(){ console.log('I'm eating'); } var dog=new Animal('Wangcai',3) var cat=new Animal('Mimi',3) dog.eat() cat.eat() console.log(dog.eat==cat.eat); </script>
We found that not only was this method successfully called, but the addresses of the two calling methods were the same. This proved that its public complex data type only opened up a memory space, reducing the number of public methods written in the constructor before. The problem of resource waste inside functions.
The function of the object prototype __proto__ is to let you figure out a question: Why can the method added to the prototype attribute of the constructor be used when instantiating the object? This is because every object has a __proto__ attribute (note that there are two underscores before and after). This attribute is also a pointer, pointing to the prototype object prototype of its corresponding constructor, which explains why the instantiated object You can call methods in the prototype object.
We should note that the function of the object prototype __protp__ is only to provide a direction for searching the content in the prototype object. We do not need to use it, we only need to remember that it points to the corresponding constructor. The prototype object prototype is
object prototype __proto__ and the prototype object prototype of the constructor function have a constructor attribute, so it is called constructor It is called a constructor because this attribute points to the corresponding constructor itself. It is mainly used to record which constructor the instantiated object refers to
<script> function Animal(name,age){ this.name=name; this.age=age; } Animal.prototype.eat=function(){ console.log('I'm eating'); } var dog=new Animal('Wangcai',4) console.log(dog.__proto__.constructor); console.log(Animal.prototype.constructor); </script>
We found that the printed result is indeed the constructor itself