How To Override A Base Class Constructor In Javascript
The Udacity ES6 training has a question about overriding a base class constructor. I've got a solution but Udacity doesn't let me get away with it. The assignment is: Create a Bic
Solution 1:
you should not be using
this.wheels = 2
this.horn = "honk honk"
when already overriding these in super constructor.
class Vehicle {
constructor(color = 'blue', wheels = 4, horn = 'beep beep') {
this.color = color;
this.wheels = wheels;
this.horn = horn;
}
honkHorn() {
console.log(this.horn);
}
}
class Bicycle extends Vehicle {
constructor(wheels = 2, horn = 'honk honk') {
super(undefined, wheels, horn);
}
honkHorn() {
super.honkHorn()
}
}
let by = new Bicycle();
by.honkHorn();
Solution 2:
class Bicycle extends Vehicle {
constructor(wheels =2, horn= "honk honk"){
super(undefined, wheels, horn)
}
honkHorn(){
super.honkHorn()
}
}
then for the test I added:
const yourBike = new Bicycle(3, "tring tring")
Although other options did provide the right answers for the test cases described in the question. By adding this extra test I found out that overriding a base class constructor is not possible from super nor from this.wheels (as was my first attempt).
However Udacity does not accept it......
Post a Comment for "How To Override A Base Class Constructor In Javascript"