// Class syntax
class Car{
constructor(name,price) {
this.name = name;
this.price = price;
}
// getter and setter
get getName(){
return this.name;
}
get getPrice() {
return this.price;
}
set setPrice(newPrice) {
this.price = newPrice;
}
}
let c1 = new Car("Audi", 10000);
console.log('c1 = \n');
console.log(c1);
console.log(`Name: ${c1.getName}`);
console.log(`Current Price: ${c1.getPrice}`);
console.log(`New Price: ${c1.setPrice=100000000}`);
console.log('New details of Car c1 = \n');
console.log(c1);
let c2 = new Car("BMW", 30000);
console.log('\n\nc2 = \n');
console.log(c2);
// OUTPUT:
//========
// c1 =
// Car {name: "Audi", price: 10000}
// Name: Audi
// Current Price: 10000
// New Price: 100000000
// New details of Car c1 =
// Car {name: "Audi", price: 100000000}
// c2 =
// Car {name: "BMW", price: 30000}
//---------------------------------------------------------------------
class RacingCar extends Car{//inheritance
constructor(name, price, maxSpeed, color) {
super(name, price); //super passes the name and price to the constructor of Class Car
this.maxSpeed = maxSpeed;
this.color = color;
}
get maximumSpeed() {
return this.maxSpeed;
}
set maximumSpeed(newSpeed){
this.maxSpeed = newSpeed;
}
}
let r1 = new RacingCar("Ferrari", 10000, 500, "blue");
console.log('\n\nr1 = \n');
console.log(r1);
console.log(`Max Speed: ${r1.maximumSpeed}`);
let r2 = new RacingCar("Bugati", 30000, 500, "white");
console.log('\n\nr2 = \n');
console.log(r2);
console.log(`New Max Speed: ${r2.maximumSpeed=300}`);
console.log('New details of Racing Car r2 = \n');
console.log(r2);
// OUTPUT:
// r1 =
// RacingCar {name: "Ferrari", price: 10000, maxSpeed: 500, color: "blue"}
// Max Speed: 500
// r2 =
// RacingCar {name: "Bugati", price: 30000, maxSpeed: 500, color: "white"}
// New Max Speed: 300
// New details of Racing Car r2 =
// RacingCar {name: "Bugati", price: 30000, maxSpeed: 300, color: "white"}