Class Definition
Use the keyword class
to create a class, and always add the constructor()
method.
The constructor method is called each time the class object is initialized.
Example A simple class definition for a class named "Car":
class Car { constructor(brand) { this.carname = brand; } static hello() { return "Hello!!"; } }
Now you can create objects using the Car class:
<script> class Car { constructor(brand) { this.carname = brand; } } mycar = new Car("Ford"); document.getElementById("demo").innerHTML = mycar.carname; </script>
Inheritance
To create a class inheritance, use the extends
keyword.
A class created with a class inheritance inherits all the methods from another class: