In addition to the usual fields and methods, a class can define static fields and methods. Unlike regular fields/properties and methods, they apply to the entire class, not to a single object.
Static fields
Static fields store the state of the class as a whole, not a single object. The name of a static field is preceded by the keyword static . For example:
class Person{ static retirementAge = 65; constructor(name, age){ this.name = name; this.age = age; } print(){ console.log(`Имя: ${this.name} Age: ${this.age}`); } } console.log(Person.retirementAge); // 65 Person.retirementAge = 62; console.log(Person.retirementAge); // 62
Here, a static field is defined in the Person class retirementAge that stores the conditional retirement age:
static retirementAge = 65;
This field applies to the entire Person class as a whole and describes the state of the entire class. After all, as a rule, there is a certain general retirement age, printed for everyone (we do not take into account individual cases for individual professions). And therefore, to refer to a static field, the name of the class is used, and not the name of any object. Using the class name, we can get or set its value:
Person.retirementAge = 62; console.log(Person.retirementAge); // 62
At the same time, we can NOT access these fields through non-static methods and the class constructor this, like the following:
print(){ console.log(`Name: ${this.name} Age: ${this.age}`); console.log(`Retirement Age: ${this.retirementAge}`); // static field cannot be accessed via this }
If we still want to refer to static fields and methods inside non-static methods and the class constructor, then again, as in the general case, you must use the class name:
print(){ console.log(`Name: ${this.name} Age: ${this.age}`); console.log(`Retirement Age: ${Person.retirementAge}`); }
Static Methods
Static methods, like static fields, are defined for the entire class as a whole, not for a single object. To define them, the static operator is placed before the method name. For example:
class Person{ constructor(name, age){ this.name = name; this.age = age; } print(){ console.log(`Name: ${this.name} Age: ${this.age}`); } static printClassInfo(){ console.log("The Person class represents a person"); } } Person.printClassInfo(); // The Person class represents a person
A static method is defined here printClassInfo(), which for simplicity just outputs some message. Unlike ordinary non-static methods, which define the behavior of an object, static methods define behavior for the entire class. Therefore, to call them, the class name is used, not the object name:
Person.printClassInfo();
Since a static method refers to the class as a whole, and not to an object, we can NOT access non-static fields / properties and methods of the object in it, like the following:
class Person{ constructor(name, age){ this.name = name; this.age = age; } print(){ console.log(`Name: ${this.name} Age: ${this.age}`); } static printAge(){ console.log(this.age); } // for static method this.age does not exist } Person.printAge(); // undefined
If it is necessary to refer to the properties of an object in a static method, then we can define a parameter in the method through which the object will be passed to the method:
class Person{ constructor(name, age){ this.name = name; this.age = age; } static print(person){ console.log(`Имя: ${person.name} Age: ${person.age}`); } } const tom = new Person("Tom", 37); const bob = new Person("Bob", 41); Person.print(tom); // Tom 37 Person.print(bob); // Bob 41
However, we can use the this word in static methods to refer to static fields and other static methods:
class Person{ static retirementAge = 65; constructor(name, age){ this.name = name; this.age = age; } print(){ console.log(`Name: ${this.name} Age: ${this.age}`); } static calculateRestAges(person){ if(this.retirementAge > person.age){ const restAges = this.retirementAge - person.age; console. log(`${restAges} years until retirement`); } else console.log("You are already retired"); } } const tom = new Person("Tom", 37); Person.calculateRestAges(tom); // 28 years left before retirement const bob = new Person("Bob", 71); Person.calculateRestAges(bob); // You are already on
A static method is defined here calculateRestAges(), which calculates how much a certain person has left before retirement. And for the calculation, it refers to the static field retirementAge:
const restAges = this.retirementAge - person.age;
Private static fields and methods
Like regular fields and methods, static fields and methods can be private. Such fields and methods are only accessible from other static methods of the class:
class Person{ static #retirementAge = 65; constructor(name, age){ this.name = name; this.age = age; } print(){ console.log(`Name: ${this.name} Age: ${this.age}`); } static calculateRestAges(person){ if(this.#retirementAge > person.age){ const restAges = this.#retirementAge - person.age; console. log(`${restAges} years until retirement`); } else console.log("You are already retired"); } } // console.log(Person.#retirementAge); // ! Error: field retirementAge is private const tom = new Person("Tom", 37); Person.calculateRestAges(tom); // 28 years left before retirement const bob = new Person("Bob", 71); Person.calculateRestAges(bob); // You are already retired
Unlike the previous example, now the static field retirementAge is private. And now it can be accessed only inside the static methods of the class.