Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

84
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda