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>
Someone please help