Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

83
Views
How does this [].fill.call({ length: 3 }, 4) example work?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!