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

164
Visualizações
How to use private properties in a method who'll be an event listener

I have a class with private properties which will be read in a method, that method will be called in an addEventListener() as a callback function. But JavaScript doesn't allow it for some reason, it throws an error:

Cannot read private member #start from an object whose class did not declare it

Using getters doesn't work either since JavaScript appears to create new undefined properties when attempting to call the getter.

Here's the code:

class Load {
  #start;
  #end;
  constructor(){
    this.#start = 0;
    this.#end = 5;
  }
  get start(){return this.#start}
  get end(){return this.#end}
  
  next(evt){
    const event = evt.target;
    event.innerText = `start: ${this.#start}. end: ${this.#end}`;
  }
}

const load = new Load();
document.querySelector('div').addEventListener('click', load.next);
div{
  width: 200px;
  padding: 50px 0;
  box-sizing: border-box;
  background-color: #eeeeee;
  text-align: center;
}
<div>click me</div>
is there a workaround for it to work or am I doing something wrong?

Someone please help

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