Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

85
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda