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

198
Views
Binding works when function is assigned as a property, but why?

I'm trying to understand how binding works in Javascript and the following example works. But when I try to use this.numbers.map(this.doSomething) it gives me an error because doSomething is being statically called. To make it work again I'll have to use the second example, but I don't understand exactly what's happening. Anyone have some links / documentation I could use to understand this a bit better?

First example:

export class SomeClass{
  numbers = [1, 2, 3];
  showNumber = (value: number) => console.log(`number is:`, value);

  processNumbers = () => {
    const a = this.numbers.map(b => this.doSomething(b));

    // this gives me the error: 'showNumber' doesn't exist
    // const a = this.numbers.map(this.doSomething);

    // do something with a
  }

  private doSomething(value: number) {
    this.showNumber(value);
  }
}

const someClass = new SomeClass();
someClass.processNumbers();

Second example:

export class SomeClass{
  numbers = [1, 2, 3];
  showNumber = (value: number) => console.log(`number is:`, value);

  processNumbers = () => {
    const a = this.numbers.map(this.doSomething);

    // do something with a
  }

  private doSomething = (value: number) => {
    this.showNumber(value);
  }
}

const someClass = new SomeClass();
someClass.processNumbers();
about 4 years ago · Juan Pablo Isaza
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!