This article has participated in the call for good writing activities, click to view: back end, big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!
Complete the prototype chain
Do not understand when the thief difficult! Understand after I fuck!
Note: This article assumes you are familiar with basic constructors and the use of JSON objects
Also: this article assumes that you can tell the function this points to the problem
But: If you’re not sure yet, read the article firstLearning This Pointing in Chinese
This article also assumes that you know what data structures are, and one-way linked lists
Let’s look at a typical one-way linked list
The above code is a typical one-way linked list
We can only find the next node by n.next
But there is no way to find the previous node from the current node
But what does this have to do with the prototype chain?
It doesn’t matter so far.
Where do the prototype objects come from?
Analyze the above code
Why can you call toString when obj is empty?
Where does this toString method come from?
Or our array object
Well, it all came from a guy called a prototype
We can print the prototype object as a.__proto__ object
This is an implicit property of all objects, which normally prints objects, and we don’t see this property
But we can still access the prototype object with the odd attribute name __proto__
This attribute name is really not easy to access, but there is actually another way to access it
This is a more formal way of accessing a function by its name
For example, look at the following example
Now we probably have some questions
- 1. Can the instance object access all properties of the prototype object?
- 2. Does each instance object have its own prototype object, or does everyone share one?
- 3. If the properties of the instance object conflict with the properties of the prototype, who will be accessed?
- 4. What do archetypal objects have to do with inheritance?
- 5. Where did the prototype chain come from?
Let’s solve a few questions in turn
1. Can the instance object access all properties of the prototype object?
Without further ado, let’s try it out and see what happens
In the code above, we create a constructor called Phone
At the same time, we added two properties to the prototype object, price and color
Playmusic and Phonecall methods are also added
Let’s access this through instance objects
As you can see, the properties and methods in the stereotype can be accessed directly by the instance object!
2. Does each instance object have its own prototype object, or does everyone share one?
This is a very simple mathematical proof. The proof is as follows:
The final conclusion is that all instance objects share the same stereotype object
3. If the properties of the instance object conflict with the properties of the prototype, who will be accessed?
Let’s try it out again
As you can see, if the object itself has this property or method, its own is preferentially accessed
If not, the attributes of the stereotype are accessed as shown below
4. What do archetypal objects have to do with inheritance?
Let me ask you one more question
If the prototype is also an object
Surely it should have its own prototype object, right?
We can access it via p1.__proto__.__proto__
Let’s draw it a little bit simpler
As you can see from the figure, as long as the prototype object exists
Object P1 has all the capabilities of the archetypal object, which we also call inheritance
And what are the relationships between these prototype objects?
p1
p1.__proto__
p1.__proto__.__proto__
p1.__proto__.__proto__.__proto__
Copy the code
The prototype chain, hence the name
Questions that need to be added
Is there really no end to prototype objects?
Of course not, the prototype object is created automatically by the browser, and of course has its own rules
Here are the rules:
1. When each constructor is created, an instance object of the function is created as the default prototype
Prototype = new Phone();
2. The prototype of this prototype Object, by default, points to Object.prototype
__proto__ = object.prototype;
3. Of course, Object.prototype is an instance of its own
Prototype = new Object();
4. However, Object.prototype no longer has a prototype Object
Object.prototype.__proto__ = null
5. Therefore, prototype objects are limited
P1. __proto__ accessible
P1. __proto__. __proto__ accessible
P1. __proto__. __proto__ __proto__ is null
What are the application scenarios of the prototype?
Charge player ability