Saturday 20 October 2018

JavaScript: Adding static properties to a class

You can add static properties to a class using class name.

Syntax
ClassName.propertyName

HelloWorld.js
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



Previous                                                 Next                                                 Home

No comments:

Post a Comment