Classes are used to create object instances
class ClassName { constructor(parameter1, parameter2, ...) { this._parameter1 = parameter1; this._parameter2 = parameter2; ... } get parameter1() { return this._parameter1; } get parameter2() { return this._parameter2; } set parameter2(newVal) { this._parameter2 = newVal; } classMethod1() { code block; } classMethod2() { code block; } ... }
class keywordconstructor method is executed every time the class is called to create an instance of itconstructor method takes parameters which will be the property values for the instance of the class createdconstructor method, you can add getter and setter methods and also other class methods that would define the class,const classInstance = new Class(parameter1, parameter2, ...);
We use the new keyword to create an instance of a class
You can create classes that inherit certain properties and methods from another class
For example, you can create InvestmentBanker and Trader classes that inherit the Financier class
> In this case, the Financier class is the parent class and the InvestmentBanker and Trader classes are each a child class
The parent class:
class Financier { constructor(name, age) { this._name = name; this._age = age; this._industry = 'Finance'; this._remainingVacDays = 25; } get name() { return this._name; } get age() { return this._age; } get industry() { return this._industry } get remainingVacDays() { return this._remainingVacDays; } takeVacation(daysOff) { this._remainingVacDays -= daysOff; } }The child class:
class Trader extends Financier { constructor(name, age, avgReturn) { super(name, age); this._avgReturn = avgReturn; this._overHoursAcc = 0; } get avgReturn() { return this._avgReturn; } get overHoursAcc() { return this._overHoursAcc; } overHours(hours) { this._overHoursAcc += hours; while (this._overHoursAcc >= 8) { this._overHoursAcc -= 8; this._remainingVacDays++; } } }
extends keyword -> this makes the methods of the parent class available in the child classconstructor method of the child class should begin with the super keyword with the parameters -> this makes the properties of the parent class available in the child classsuper keyword, we then add the properties specific to the child classsuper keyword must come before any this keyword* in the subclass's constructorextends keyword automatically brings all of the parent class's methods available to the child class, after the constructor method, we only need to add additional methods that apply just to the child classYou can set certain methods to be static using the static keyword
Static Methods are methods that can only be called directly from the class, but cannot be called from an instance of the class
class Student { constructor(name, age) { this._name = name; this._age = age; } get name() { return this._name; } get age() { return this._age; } static generateId() { let randomId = Math.floor(Math.random() * 10000); return randomId; } }
- The
generateId()method can only called from theStudentclass directly. It cannot be called from any instance of the classconsole.log(Student.generateId()); // Prints a random number between 0 and 9,999const studentJohn = new Student('John', 15); console.log(studentJohn.generateId()); // results in TypeError