T3A14-Extend-Classes-by-Abhay Version 1.6.13

T3A14-Extend-Class-by-Abhay.html

Good reference to Javascript extend classes.
Extending a pre-existing Class

Example Class extend code


 class myCat extends myAnimal {                                                                      
     myShow(){
       document.getElementById('myDiv01').innerHTML = this.myName
    }                                                                   
}

Example working Classes code

Example extending Class code

...


The "extends" keyword inherits all methods from another class.

The "super" method calls the parent's constructor function.


The "extends" keyword inherits all methods from another class.
The "super" method calls the parent's constructor function.

<p id="ytj"></p>

<script>
class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'My favourite food is ' + this.carname;
  }
}

class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is ' + this.model;
  }
}

mycar = new Model("Indian", "Chicken Curry");
document.getElementById("ytj").innerHTML = mycar.show();
</script>



A static method is created with the "static" keyword, and you can only call the method on the class itself.


<p id="hj"></p>

<script>
class Car {
  constructor(brand) {
    this.carname = brand;
  }
  static hello() { 
    return "Hello!, this text is written using static class.";
  }
}

mycar = new Car("Ford");


document.getElementById("hj").innerHTML = Car.hello();


</script>