What is the meaning of __proto__ and prototype in JS?

Insert picture description here
Insert picture description here
When your JS code is not running, there is already a window object in the JS environment. The window object has an Object property, and window.Object is a function object window.Object This function object has an important property called prototype, what is it used for, etc. It will be said that window.Object.prototype has such several properties toString (function) and valueOf (function). It is enough to know these first. Then we write a code var obj = {}
obj.toString() What does this code do? Why does obj have a toString() attribute?
Insert picture description here
This sentence probably makes the obj variable point to an empty object, and this empty object has a proto attribute pointing to window.Object.prototype. So when you call obj.toString(), obj itself does not have toString, so go to obj. proro to find toString. So when you call obj.toString, you actually call window.Object.prototype.toString. So how does window.Object.prototype.toString get the content of obj? That's because obj.toString() is equivalent to obj.toString.call(obj) and obj.toString.call(obj) is equivalent to window.Object.prototype.toString.call(obj) This sentence passes obj to toString Up

Insert picture description here
We write a code

ARR = var []
arr.push (. 1) // [. 1]
Will two words did
Insert picture description here
.

Look at the red part, var arr = [] probably makes arr point to an empty object, and then arr. proto points to window.Array.prototype. (In fact, arr has a length:0, but ignore it here) So when you call arr.push, arr itself does not have a push attribute, so go to arr. proto to find push, so arr.push is actually window.Array. prototype.pusharr.push(1) is equivalent to arr.push.call(arr,1) arr.push.call(arr,1) is equivalent to window.Array.prototype.push.call(arr, 1) and then A little more complicated. What does arr.valueOf() do? Arr itself does not have valueOf, so go to arr. proto to find arr. proto only pop, push and no valueOf, so go to arr. proto . Proto to find arr. proto . ProtoIt is window.Object.prototype so arr.valueOf is actually window.Object.prototype.valueOfarr.valueOf() is equivalent to arr.valueOf.call(arr) arr.valueOf.call(arr) is equivalent to window.Object.prototype .valueOf.call(arr) Look, JavaScript is actually very beautiful and simple. It’s just that you want to be complicated: prototype points to a piece of memory, and there are shared properties in this memory. __proto__ points to the same piece of memory . The difference between prototype and proto is that prototype is the property of the constructor, and proto is the property of the object. The difficulty lies in...constructor It's also an object! If there is no prototype, there is no place for shared properties. If there is no proto , then an object does not know what its shared properties are. By contradiction, suppose we remove the proto , then var obj = {}
obj.toString() // An error is reported, there is no toString method, so you can only declare an object like this: var obj = { toString: window.Object.prototype.toString, valueOf : window.Object.ptototype.valueOf } obj.toString() //'[object Object]'



Do you know how much code proto saves for you? Suppose we delete prototype, including window.Object.prototype and window.Array.prototype. Then window.Object.prototype.toString has also been deleted. Then we basically can't write code... var obj = {}
obj.toString() // toString does not exist, because toString has not been defined before. The meaning of prototype is to pre-define the common properties for subsequent objects. Think about it for yourself~ For
example, many people don’t understand what a pseudo-array is. It’s very simple: if the proto of an array directly or indirectly points to Array.prototye (using the shared attributes of the array), then it is a true array. If the proto of an array is not directly Or indirectly pointing to Array.prototye, then it is a pseudo-array var realArr = {0:'a', 1:'b', length: 2}
realArr. proto = Array.prototype
// This is a real array (not exactly)
// Basically equivalent to realArr = ['a','b']
realArr.push !== undefined // true

var fakeArr = {0:'a', 1:'b', length: 2}
// This is the pseudo array
realArr.push === undefined // true

Guess you like

Origin blog.csdn.net/qq_45424679/article/details/114776754