Why String.prototype.__proto__ = = Object.prototype?

it is known that _ _ proto__ points to the prototype of the object constructor, while the constructor of String.prototype should be String instead of Object, so why String.prototype.__proto__ = = Object.prototype is true?

Jun.07,2021

disagrees with the above answer, mainly whenever it is an object, the following expression holds the sentence

the question you ask is the prototype chain, which can be searched casually on the Internet. Ask here, few people will tell you a lot, and it is clear and clear, so it is best to search the Internet and thoroughly understand this

.

the prototype chain can be divided into three roles: constructor, parent object, and child object

The

constructor is a function, such as Object, Function, String, Array and even the most common custom function. When the constructor is created, it creates an object in memory at the same time, that is, the prototype object. We can use the function .prototype to access this object

. The

child object is derived from the constructor new. While new, the child object will inherit the parent object, that is, the prototype object, which is embodied in the child object. _ _ proto__ = parent object

after understanding the triangular relationship above, it is not difficult to understand the prototype chain

prototype chain can be regarded as a grandchild chain, the top layer is null (this is a problem left over by js history), the third layer of Object.prototype, is Function.prototype, String.prototype, etc.

when we create an object, we can use var a = {} or var a = new Object () then an object inherits from Object.prototype, which may be the basis of the above guys, but when we create an object b inherits from a, then b and Object.prototype are grandchildren.

in addition, the array is also an object, function a () {} , where an is a function and inherits from Function.prototype , then he also has a grandson relationship with Object.prototype

.

I won't talk about it any more here. It's not something that can be explained in a few words, but it's the same sentence. Go to the Internet to check the information and check it out in many ways

.

here I recommend an article I wrote earlier on prototype prototype chain in terms of data type , you can see the prototype chain section at the end


regardless of what String.prototype points to, String.prototype refers to the literal quantity of an object

String.prototype instanceof Object //true

whenever it is a literal quantity of an object, the following expression holds

.
obj.__proto__ == Object.prototype //true

to sum up

String.prototype.__proto__ == Object.prototype //true

I don't want to say that much here, but it's not as complicated as he said.
I recommend this book to you. ide
end

Menu