Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!