Почему я не могу использовать функцию жирной стрелки в Object.define

Почему я не могу использовать функцию жирной стрелки в Object.define()?

Минимальный, полный и проверяемый пример

Работает

class Car {
  constructor(color) {
    this.color = color;
  }
}
Object.defineProperty(Car.prototype, 'getColor', {
  value: function() { // <--- ******** HERE ***********************
    return this.color
  }
  , writable:false
  , configurable: true
  , enumerable: true
})
const redCar = new Car('red');

console.log(redCar.getColor()); //-> red

Сломанный

class Car {
  constructor(color) {
    this.color = color;
  }
}
Object.defineProperty(Car.prototype, 'getColor', {
  value: () => { // <--- DOES NOT WORK
    return this.color
  }
  , writable:false
  , configurable: true
  , enumerable: true
})
const redCar = new Car('red');

console.log(redCar.getColor()); //-> undefined

0 ответов

Другие вопросы по тегам