You
can add static properties to a class using class name.
Syntax
ClassName.propertyName
class Employee{ constructor(firstName, lastName){ this.firstName = firstName; this.lastName = lastName; Employee.noOfEmployees++; } } Employee.noOfEmployees = 0; var emp1 = new Employee("Krishna", "Gurram"); var emp2 = new Employee("Gopi", "Battu"); console.log("Total objects : " + Employee.noOfEmployees);
Output
Total
objects : 2
No comments:
Post a Comment