A series of important questions associated with Array's __proto__ and prototype?

Fuse :

We use this line of code when converting the arguments array-like to an array-like:

var arg = Array.prototype.slice.call(arguments)

So why Array.prototype.slice.call, not Array.slice.call

They are all on a prototype chain, shouldn't it be possible to automatically find calls from bottom to top? // naive idea

Wrong, not on a prototype chain

The theory is this, I can’t find the method to call Array, and then go to Array.__proto__ instead of Array.prototype

and:

Array.__proto__ === Array.prototype
//false

Then let me think about what Array.__proto__ is? is Object.prototype?

Array.__proto__===Object.prototype
//false,也不对

Array is a constructor, then it is an instance of Function, it should be like this:

Array.__proto__ === Function.prototype
//true

back to the original question

In this way, we can understand why Array.slice.call cannot be used, because although Array is a constructor, it is also a function object. There is _ proto__, and slice cannot be found in Array. Of course, there is no one in _ proto__ of Array. .

The slice is in Array.prototype.

two important points

1. When you can't find the attribute, go to __proto__ to find it, not .prototype.

2. Constructors are also function objects, don't forget that they are instances of Function.

3. Only constructors have prototypes.

Guess you like

Origin blog.csdn.net/qq_42533666/article/details/129115235