es6 class inheritance code example
Example 1: javascript class inheritance
class Mammal {
constructor(name) {
this.name = name;
}
eats() {
return `${this.name} eats Food`;
}
}
class Dog extends Mammal {
constructor(name, owner) {
super(name);
this.owner = owner;
}
eats() {
return `${this.name} eats Chicken`;
}
}
let myDog = new Dog("Spot", "John");
console.log(myDog.eats());
Example 2: es6 class example
<script>
class Student {
constructor(rno,fname,lname){
this.rno = rno
this.fname = fname
this.lname = lname
console.log('inside constructor')
}
get fullName(){
console.log('inside getter')
return this.fname + " - "+this.lname
}
}
let s1 = new Student(101,'Sachin','Tendulkar')
console.log(s1)
console.log(s1.fullName)
</script>
Example 3: es6 class example
class Polygon {
constructor(height, width) {
this.height = height;
this.width = width;
}
}