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();