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

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