i was learning about inheritance in a course however i found the super keyword on MDN, i started implementing it, and it sort of works, it will return the two properties that are also in the parent class,but the additional property i try to implement in the child class always returns undefined, how can i impelement this to add properties in the correct way, as i come to understand it which i could be wrong, is super applies the this keyword to the parent class. here's my code...
class Car {
constructor(speed, make){
this.speed = speed,
this.make = make
}
get speed() {
return this._speed / 1.6;
}
set speed(mov){
this._speed = mov * 1.6
}
accel(){
console.log(this.speed += 5);
}
}
const chevy = new Car(120, 'chevy');
//challenge 3
class Ev extends Car{
constructor(speed,make,bat){
super(speed,make)
this.bat
}
billy(){
console.log('hi');
}
chargeBat(charge){
this.bat = charge;
return `ur battery is charging to ${charge}`;
}
cel(){
this.speed += 20;
this.bat - 1;
return `your car is going ${this.speed} and battery is ${this.bat}`
}
}
// Ev.protype = Object.create(Car.prototype);
const prius = new Ev(35,'prius',99);
// console.log(prius.chargeBat());
// console.log(prius.cel())
console.log(prius.bat);
console.log(prius.speed)
You throw away the bat value in the Ev constructor. Store it in this.bat to correct that. See code below.
class Car {
constructor(speed, make){
this.speed = speed,
this.make = make
}
get speed() {
return this._speed / 1.6;
}
set speed(mov){
this._speed = mov * 1.6
}
accel(){
console.log(this.speed += 5);
}
}
const chevy = new Car(120, 'chevy');
//challenge 3
class Ev extends Car{
constructor(speed,make,bat){
super(speed,make)
this.bat = bat; // <------------- HERE
}
billy(){
console.log('hi');
}
chargeBat(charge){
this.bat = charge;
return `ur battery is charging to ${charge}`;
}
cel(){
this.speed += 20;
this.bat - 1;
return `your car is going ${this.speed} and battery is ${this.bat}`
}
}
// Ev.protype = Object.create(Car.prototype);
const prius = new Ev(35,'prius',99);
// console.log(prius.chargeBat());
// console.log(prius.cel())
console.log(prius.bat);
console.log(prius.speed)