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

156
Vistas
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 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