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

200
Visualizações
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
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