Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda