为什么我不能在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