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

311
Vistas
Function binding does not work, returns undefined

so I have created two objects: person and Tim.

I would like to bind logInfo function to the Tim but when I call this with binding, it keeps giving me:

  1. undefined job
  2. undefined phone

Here is the code:

function hello() {
console.log('Hello')
}

const person = {
firstNAme: 'A',
age: 26,
sayHello: hello,
sayHelloWindow: hello.bind(document),
logInfo: function (job, phone) {
    console.group(`${this.firstNAme} info: `)
    console.log(`name is: ${this.firstNAme} and the age is: ${this.age}`)
    console.log(`Job is: ${this.job}`)
    console.log(`Phone is: ${this.phone}`)
    console.groupEnd()
  }
}

const Tim = {
firstNAme: 'Tim',
age: 22
}

const infoTim = person.logInfo.bind(Tim)
infoTim('clown', '100100-10010')  // returns undefined values, why?

Do I seem to miss something? Thank you in advance!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

In logInfo function, remove this for job and phone as they are being accepted via arguments and are not property of Tim object. this always refers to current object and this.something points to property of the object .

Refer updated code

   function hello() {
      console.log('Hello')
   }
    
    const person = {
    firstNAme: 'A',
    age: 26,
    sayHello: hello,
    sayHelloWindow: hello.bind(document),
    logInfo: function (job, phone) {
        console.group(`${this.firstNAme} info: `)
        console.log(`name is: ${this.firstNAme} and the age is: ${this.age}`)
        console.log(`Job is: ${job}`)
        console.log(`Phone is: ${phone}`)
        console.groupEnd()
      }
    }
    
    const Tim = {
    firstNAme: 'Tim',
    age: 22
    }
    
    const infoTim = person.logInfo.bind(Tim)
    infoTim('clown', '100100-10010')
about 4 years ago · Santiago Trujillo 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