I have a code in which I declare a private class member using "#" and then initialize it in the constructor and then when I try to set / access the value I get the following error
Uncaught TypeError: Cannot read private member #mouse from an object whose class did not declare it
at mouseDownEvent
where the mouseDownEvent is the function, where I try to access the values
code example: ( refer to the EDIT code )
class Testing
{
#property;
constructor()
{
this.#property = new Vector2();
}
mouseDownEvent()
{
this.#mouse.x = somevalue; <- error is here
}
}
EDIT
class Testing
{
#mouse;
constructor()
{
this.#mouse= new Vector2();
}
mouseDownEvent()
{
this.#mouse.x = somevalue; <- error is here
}
}
Since you are passing the mouseDownEvent function to an event listener, the value of this will not be this Testing instance when it is actually invoked. So you need to bind it to this instance when passing it to the event handler as below.
window.addEventListener("mousedown", mouseDownEvent.bind(this)(), false)
You can read further about Javascript bind() from below link. https://www.javascripttutorial.net/javascript-bind/