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

127
Vistas
How to add property to object with constructor to be used by listener method?

I have a module function which sets up an addEventListener function and currently uses a global variable in the addEventListener function. I want to encapsulate this in an Object. But I can't see how to access the property from within the addEventListener function because 'this' within the addEventListener doesn't refer to my object ?

i.e

 let x=0;
 function mine (){
    window.addEventListener('click',listener);
    function listener(){
         dosomething with x;
    }
 } 
 function setX(y) {x=y};
 export {setX};

to

function Mine() {
  this.x=0;
  window.addEventListener('click',listener);
  function listener(){
    dosomething with this.x;
  }
}
Mine.prototype.setX = function(y) {this.x=y);
export {Mine};

the latter does not work because in a listener 'this' does not refer to 'Mine' (I think).

Any help appreciated.

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