What is the principle of converting pseudo arrays to true arrays by call and apply?

you can often see the method of converting a pseudo array into a true array on the Internet; similar to

let pretendArr = {0:0,1:1,2:2,length:3};
[].slice.call(pretendArr); //[0,1,2]

so why? Why can it be converted to a true array? I hope someone can help solve the confusion

Jan.17,2022

this article introduces
https://www.cnblogs.com/littl.

in great detail.

[] .slice.call is actually equivalent to Array.prototype.slice.call
[] is a Array that calls the method
clipboard.png

on the array prototype.

wrote an article
on this question. I was halfway through yesterday afternoon. I just made up the second half after I got home from work.


is not converted by call, it is done by Array.prototype.slice.
because there is no slice method on Object.prototype.


my understanding of call and apply is to graft other people's things onto another person.

pretendArr is an object, [] is an empty array, if you pretendArr.slice directly, it will report an error, but [] this object has a prototype method of slice. So I grafted the slice method of the [] array object to the pretendArr object through the call method, and I can implement the method that the obj object uses the array object.

in addition, [] .slice.call () is a bit coquettish

Menu