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

86
Visualizações
Can't changing the value of JavaScript property

class Snake {
    snakeDirection = 'ArrowRight';

    constructor() {
        setInterval(() => {
            this.moveSnake();
        }, 800);

        document.addEventListener('keydown', this.changeDirection);
    }

    moveSnake() {
        console.log('moveSnake', this.snakeDirection) //ArrowRight
    }

    changeDirection(e) {
        this.snakeDirection = e.key;
        console.log('key pressed', e.key)
    }
}

let a = new Snake();

I'm new to javscript and creating a small project using OOPs concept. I am trying to change the value of snakeDirection by calling changeDirection method but it's can't get updated in moveSnake method which is gets called every 800ms. How will I do this?

class Snake {
    snakeDirection = 'ArrowRight';

    constructor() {
        setInterval(() => {
            this.moveSnake();
        }, 800);

        document.addEventListener('keydown', this.changeDirection);
    }

    moveSnake() {
        console.log('moveSnake', this.snakeDirection) //ArrowRight
    }

    changeDirection(e) {
        this.snakeDirection = e.key;
        console.log('key pressed', e.key)
    }
}

let a = new Snake();

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

0

You must pass an argument (event) to func.

const snake = new Snake();
document.addEventListener('keydown', e => snake.changeDirection(e));
about 4 years ago · Juan Pablo Isaza Relatório

0

This is the problem that I have spotted this.snakeDirection cant be used because it is not in the constructor method. use just snakeDirection = e.key. I mean that it is not the property of the class, it is just a class variable that can't be used as a class property. Class variables and class properties are two different concepts.

about 4 years ago · Juan Pablo Isaza Relatório

0

It's a context issue with setInterval. If you change your methods to class properties (basically arrow functions) so that this is understood properly the problem disappears.

class Snake {

  snakeDirection = 'ArrowRight';
  
  constructor() {
    document.addEventListener('keydown', this.changeDirection);
    setInterval(this.moveSnake, 800);
  }
  
  moveSnake = () => {
    console.log('moveSnake', this.snakeDirection);
  }

  changeDirection = (e) => {
    console.log(e.key)
    this.snakeDirection = e.key;
  }

}

let a = new Snake();

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