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

305
Views
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 answers
Answer question

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 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!