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

305
Visualizações
Uncaught TypeError: can't set private field: object is not the right class

I have a class called Timer:

class Timer {
  #tick() {
    console.error('Error usage of interface Timer explicitly deleted');
  }

  startTimer() {
    console.error('Error usage of interface Timer explicitly deleted');
  }
}

And I have a class called CyclingTimer which derives from Timer:

class CyclingTimer extends Timer {
  #maximumTime
  #timeBetweenTicks
  #currentTime
  #onTick

  constructor(maximumTime, timeBetweenTicks) {
    super();    

    this.#maximumTime = maximumTime;
    this.#timeBetweenTicks = timeBetweenTicks;

    this.#currentTime = 1;
  }

  #validateBoundaries() {
    if(this.#currentTime > this.#maximumTime) this.#currentTime = 1;
    else if(this.#currentTime < 1) this.#currentTime = this.#maximumTime;
  }

  #tick() {

    //I console.log(this) here to debug and it printed the Window object as this. I don't know why

    this.#currentTime++; //Error Occures In This Line
    this.#validateBoundaries();
    
    this.#onTick();
  }

  startTimer() {
    //When I console.log(this) here it prints CyclingTimer as expected
    setInterval(this.#tick, this.#timeBetweenTicks);
  }

  setOnTickFunction(callbackFunction) {
    this.#onTick = callbackFunction;
  }
}

I have no clue why this happens and these are the only things I have. When I searched about the error I found nothing.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I finally solved the issue. The problem was when using setInterval() 'tick()' lost its 'this' to solve this issue I bind 'tick()' to my object than pass binded function to setInterval()

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