class Car {
constructor(brand) {
this._brand = brand;
}
get brand() {
return this._brand;
}
set brand(newBrand) {
if (newBrand.length > 0) {
this._brand = newBrand;
} else {
console.log("Brand name cannot be empty");
}
}
}
const myCar = new Car("Toyota");
// myCar.brand = "Honda"; // Menggunakan setter untuk mengubah brand
// console.log(myCar.brand); // Output: "Honda"
myCar.brand = ""; // Gagal mengubah karena ada validasi
console.log(myCar.brand);
// Output: "Brand name cannot be empty"