我有一個如下所示的類:export default class Car { brand = ''; color = ''; fuel = ''; constructor(car) { ... }}如何在類變量上設(shè)置 setter?例如export default class Car { brand = ''; color = ''; fuel = ''; set brand(brand) { ... } get brand() { return ...; } get color(color) { return ...; } set color(color) { ... }}我嘗試使用上面的方法,但是它不起作用。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';展開片段它不會提醒我設(shè)置的值brand。
在類變量 JavaScript 上設(shè)置 getter/setter
料青山看我應(yīng)如是
2023-01-06 15:50:18