0

The problem here is that when z is invoked it prints undefined. But when I change the declaration of variable x with var x, instead of let x, it prints 10. So, what is the difference when using 'this' keyword with var and let in this code.

let x = 10;
let y = {
  x: 90,
  getX: function() {
    return this.x;
  }
}

console.log(y.getX()); // 90
let z = y.getX;
console.log(z()); // undefined
Andreas
  • 21,535
  • 7
  • 47
  • 56

0 Answers0