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

196
Visualizações
How to change `this` in a lambda function returned by a getter

I have defined a class containing a getter returning a lambda function. I'd like to change the "this" used in the function returned, but I did not find any way to do that. I made many tries with function.call but no matter what, I did not get the expected result. So I need help!

Here is some minimal code reproducing the problem:

class A {
    get fn() { return () => [this.name, this]; }
}
let a = new A();
a.name = "a";
let b = { name: "b" };
window.console.log(a.fn.call(b)); // How to get "b" ?

The result is :

[
  "a",
  {
    "name": "a"
  }
]

How to apply the returned lambda function to b ? and receive the expected result:

[
  "b",
  {
    "name": "b"
  }
]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to take a standard function instead of an arrow function.

class A {
    get fn() { return function () { return [this.name, this]; } }
}
let a = new A();
a.name = "a";
let b = { name: "b" };

console.log(a.fn.call(b)); // How to get "b" ?

about 4 years ago · Juan Pablo Isaza 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