I have been using Classes for ease of understand prototypes. It makes logical sense that extending a class lets you inherit from that base class. How would I do the below code using function constructors though? :
class Person {
constructor(name,age,speciality="Not Specified") {
this.name = name;
this.age = age;
this.speciality = speciality;
}
sayName() {
return this.name;
}
}
class Athlete extends Person {
constructor(name, age, speciality) {
super(name,age)
this.speciality = speciality
}
getSport() {
return "I play " + this.speciality;
}
}
let john = new Person('john', 44)
let jim = new Athlete('jim', 54, 'basketball');
I know I would have:
function Person(name,age,speciality="Not Specified") {
this.name = name;
this.age = age;
this.speciality = speciality;
}
I assume I would have sayName()
as a prototype rather than a property inside Person like:
Person.prototype.sayName = function() {
return this.name;
}
but then how would I have an Athlete constructor that inherits these like I can do with extending a class?