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

127
Vistas
Pasar expresión de función a objeto devuelve indefinido

He revisado preguntas similares y la mayoría parece estar preocupada por this , la mía no. Estoy tratando de crear una nueva property que es una function expression en objetos en una matriz, pero la property no está definida. Lo necesito para devolver function

Aquí hay una versión de violín .

 let athleteArray = [ rupert = { name: 'Mike', age: '33', sport: 'sitting', quote: 'Hello Super nintendo chalmers' }, alice = { name: 'Bill', age: '35', sport: 'cooking', quote: 'stop that' } ]; const win = function(name, sport) { console.log(`${name} won the ${sport} event!`) } //loop to add new 'win' property for (let i = 0; i < athleteArray.length; i++) { athleteArray[i].win = win(athleteArray[i].name, athleteArray[i].sport) console.log(athleteArray[i]) console.log(typeof(athleteArray[i].win)) }

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Use una clase donde pueda definir sus métodos.

 class Person { constructor(details) { this.name = details.name || 'Unknown'; this.age = details.age || 'Unknown'; this.sport = details.sport || 'Unknown'; this.quote = details.quote || 'Unknown'; } win () { return `${this.name} won the ${this.sport} event!`; } } const athleteArray = [ new Person({ name: 'Mike', age: '33', sport: 'sitting', quote: 'Hello Super nintendo chalmers' }), new Person({ name: 'Bill', age: '35', sport: 'cooking', quote: 'stop that' }) ]; athleteArray.forEach(person => console.log(person.win()));

Si quieres hacerlo a tu manera, necesitas crear una función. Solo asignas lo que devuelve a la propiedad

 let athleteArray = [ rupert = { name: 'Mike', age: '33', sport: 'sitting', quote: 'Hello Super nintendo chalmers' }, alice = { name: 'Bill', age: '35', sport: 'cooking', quote: 'stop that' } ]; const win = function(name, sport) { console.log(`${name} won the ${sport} event!`) } //loop to add new 'win' property for (let i = 0; i < athleteArray.length; i++) { athleteArray[i].win = () => win(athleteArray[i].name, athleteArray[i].sport) console.log(athleteArray[i]) console.log(typeof athleteArray[i].win) athleteArray[i].win(); }

about 4 years ago · Juan Pablo Isaza Denunciar

0

Tienes que devolver algo de la función win:

 let athleteArray = [ rupert = { name: 'Mike', age: '33', sport: 'sitting', quote: 'Hello Super nintendo chalmers' }, alice = { name: 'Bill', age: '35', sport: 'cooking', quote: 'stop that' } ]; const win = function(name, sport) { return `${name} won the ${sport} event!`; } //loop to add new 'win' property for (let i = 0; i < athleteArray.length; i++) { athleteArray[i].win = win(athleteArray[i].name, athleteArray[i].sport) console.log(athleteArray[i]) console.log(typeof athleteArray[i].win ) }

about 4 years ago · Juan Pablo Isaza Denunciar

0

La función 'ganar' no devuelve nada. La forma en que escribió el código, 'athleteArray[i].win' no estará definida. Para hacer lo que quieres, debes hacer algo como esto:

 athleteArray[i].win = () => win(athleteArray[i].name, athleteArray[i].sport)

Es más fácil entender el error si el código fuera así:

 const returnedValue = win(athleteArray[i].name, athleteArray[i].sport) // Returned value is undefined athleteArray[i].win = returnedValue
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