Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#examples Example to understand: [].fill.call({ length: 3 }, 4) //{0: 4, 1: 4, 2: 4, length: 3}
I think 'length' as a key in an object is somehow special, I can't find a reference for that assertion. If you use .fill on an empty array it would not modify the array. So, I don't understand why we are getting this object back.
First, let's look into the call() method. The first argument {length: 3} is the value to use as this when calling fill.
Then, let's look into the fill() function. When you check the Polyfill which explain how the method is implemented, you'll see the following
var O = Object(this);
// Steps 3-5.
var len = O.length >>> 0;
so, it use the length of this object to decide how to fill the array. As we
already know, this now refers to object {length: 3}, so len equals to 3. Then fill function calculates the finalValue and add properties to the object, and return the object
// Step 12.
while (k < finalValue) {
O[k] = value;
k++;
}
// Step 13.
return O;
So, you get {0: 4, 1: 4, 2: 4, length: 3}