Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda