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

232
Views
¿Cómo se pasan los argumentos en las funciones de devolución de llamada?

Comencé a aprender JavaScript para el desarrollo web y actualmente estoy atascado en un punto de la función de devolución de llamada. El problema es que no puedo entender cómo se pasan los argumentos en JavaScript.

CÓDIGO:

 const arr = [1, 2, 3, 4, 5, 6, 7, 8]; function myfunc(value){ //i had set a parameter 'value' console.log(value); // i had printed the 'value' } arr.forEach(myfunc); // i had not passed any argument in myfunc

Estoy realmente confundido acerca de cómo myfunc (valor) obtiene el parámetro 'valor' en la función forEach o cualquier función como:

 const numbers1 = [45, 4, 9, 16, 25]; function myFunction(value) { //myFunction has parameter 'value' return value * 2; } const numbers2 = numbers1.map(myFunction); /* here, how value arguments are passed to myFunction? */
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Se pasan porque forEach tiene algún código que los pasa. La implementación de forEach se verá así:

 forEach(callback) { // `this` is the array we're looping over for (let i = 0; i < this.length; i++) { callback(this[i], i, this); } }

Entonces, forEach maneja la lógica de recorrer la matriz, y luego, para cada elemento de la matriz, llamará a su función y pasará tres parámetros (el valor, su índice y la matriz completa). Simplemente escriba una función que use esos valores de la manera que lo necesite. Si no necesita usar los 3 parámetros, simplemente puede omitir cualquier argumento a la derecha de los que necesita.

about 4 years ago · Juan Pablo Isaza Report

0

Las extensiones funcionales en JavaScript Array requieren una function como argumento. Esa función puede ser:

  • Una función con nombre
 function doSomething() {} // This is a named function
  • Una función anónima
 // This is an anonymous function because we didnt give a name to it [1,2,3].forEach(function (value) { console.log(value) })
  • Una función de flecha gruesa (expresión lambda).
 // It's called fat arrow because well, the arrow is fat [1,2,3].forEach((value) => console.log('hey', value))

La implementación de las extensiones funcionales en Array siempre pasa tres argumentos: el valor, el índice y la matriz a la que se aplica la función.

La forma en que funcionan los argumentos de función en JS es que si pasa más de los argumentos requeridos a una función, JS simplemente eliminará el resto, y si pasa más de los necesarios, tendrán un valor undefined a menos que haya especificado un valor predeterminado. para esos

 const array = [1,2,3] // I am just getting the value and the name "value" could be any name array.forEach((value) => console.log(value)) // here my fat-arrow function takes two parameters, since forEach passes three parameters we're good to go array.forEach((value, index) => console.log(value, 'at', index)) // Here we're using all arguments without dropping any array.forEach((value, index, array) => console.log(value, index, array)) // that is because the forEach predicate only passes three arguments and hence the last is undefined array.forEach((value, index, array, fourth) => console.log('here', fourth, 'is always undefined'))
about 4 years ago · Juan Pablo Isaza Report

0

Esto tiene sentido para el OP, probablemente:

 const numbers1 = [45, 4, 9, 16, 25]; numbers1.forEach(e => console.log(e));

Así que probablemente esto también tenga sentido:

 const numbers1 = [45, 4, 9, 16, 25]; const someFunction = e => console.log(e); numbers1.forEach(someFunction);

Si es así, entonces esto también tendrá sentido:

 function someFunction(e) { console.log(e)); } numbers1.forEach(someFunction);
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!