What does Array.prototype here do?

If we have an object like so

let obj = {
    0: "a",
    1: "b",
    length: 3
} 

We can join its values using Array.prototype.join.call(obj, ", "). Using only join() will not work. So what exactly does Array.prototype do? I have perused MDN documentation about it, as well as call() and still don’t have a clear idea of why the above code works.

Array.prototype is a constructor which allows methods to be added to the Array object:

So, when you use a method like .slice() for instance, it’s because someone has built that method onto the Array object as Array.protoype.slice() so that you can use it on any array, by appending the method to the name of your array.

To explain your specific example more clearly:
Array.prototype.join() is an array method which simply concatenates the elements of an array into a string.

The Function.prototype.call() method is used in this instance to populate an array (which is returned as a string using join) with values from the object.

That just made it click for me, thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.