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

190
Views
Error while setting the value of a global private class property in javascript

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
   }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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/

about 4 years ago · Juan Pablo Isaza Report
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!