Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda