I have a class which looks like below:
export default class Car {
brand = '';
color = '';
fuel = '';
constructor(car) {
...
}
}
How can I set a setter on the class variables?
So for example
export default class Car {
brand = '';
color = '';
fuel = '';
set brand(brand) {
...
}
get brand() {
return ...;
}
get color(color) {
return ...;
}
set color(color) {
...
}
}
I tried to use the above, however, it doens't work.
class Car {
brand = '';
color = '';
constructor(car) {
this.brand = car.brand;
this.color = car.color;
}
set brand(val) {
// It should alert Audi.
alert(val);
}
}
let car = new Car({brand: 'BMW', color: 'white'});
car.brand = 'Audi';
It doens't alert the value I am setting on brand.