0

In the first case, I use an arrow function for getColor, and the second case I use an anonymous function. I expected that I have the same result for both cases, but not:

Case 1:

const Car = {
    color: 'blue',
    getColor: function () { console.log(this.color); }
};

Car.getColor(); // I get 'blue'

Case 2:

const Car = {
    color: 'blue',
    getColor: () => console.log(this.color)
};

Car.getColor(); // I get undefined

Is there any concept I don't understand on arrow functions?

Jils
  • 723
  • 4
  • 12
  • 29

0 Answers0