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

81
Views
¿Cómo funciona este ejemplo de [].fill.call({ length: 3 }, 4)?

Fuente: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill#examples Ejemplo para entender: [].fill.call({ longitud: 3 }, 4) //{0: 4, 1: 4, 2: 4, longitud: 3}

Creo que la 'longitud' como clave en un objeto es de alguna manera especial, no puedo encontrar una referencia para esa afirmación. Si usa .fill en una matriz vacía, no modificará la matriz. Entonces, no entiendo por qué estamos recuperando este objeto.

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

0

Primero, veamos el método call() . El primer argumento {length: 3} es el valor a usar como this cuando se llama a fill .

Luego, echemos un vistazo a la función fill() . Cuando revise el Polyfill que explica cómo se implementa el método, verá lo siguiente

 var O = Object(this); // Steps 3-5. var len = O.length >>> 0;

entonces, usa la longitud de this objeto para decidir cómo llenar la matriz. Como ya sabemos, this ahora se refiere al objeto {length: 3} , por lo que len es igual a 3. Luego, la función de fill calcula el finalValue y agrega propiedades al objeto, y devuelve el objeto

 // Step 12. while (k < finalValue) { O[k] = value; k++; } // Step 13. return O;

Entonces, obtienes {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!