Static Properties and Class Methods in JavaScript. Code exam... open

Static Properties and Class Methods in JavaScript. Code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

In addition to ordinary and private properties and methods, a class can define static fields and methods. Unlike conventional fields/properties and methods, they refer to the entire class, not to a single object.

Static properties of objects

Static properties store the state of the class as a whole, not of an individual object. The name of the static field is preceded by the static keyword. For example:


    class Person {

        //for example, let's create a static method that will contain a conditional retirement date:
        static fullage = 18;

        constructor(name, age) {
            this.name = name;
            this.age = age;
        }

        print() {
            console.log(`Name: ${this.name}  Age: ${this.age}`);
        }

    }

    console.log(Person.fullage); // 18

The fullage field refers to the entire Person class as a whole and describes the state of the entire class as a whole.

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.fullage = 21;
    console.log(Person.fullage); //21
At the same time, we can NOT access these fields through this in non-static methods and the class constructor.

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(`Full age: ${Person.fullage}`);
    }

Static Object Methods

Static methods, like static fields, are defined for the class as a whole, not for a single object. For their definitions, the static operator is placed before the method name:

    class Person {
       
        constructor(name, age) {
            this.name = name;
            this.age = age;
        }

        print() {
            console.log(`Name: ${this.name}  Age: ${this.age}`);
        }

        static printHello() {
            console.log("Hello");
        }
    }
    Person.printHello();//Hello

Unlike normal non-static methods, which define the behavior of an object, static methods define behavior for the entire class. Therefore, the name of the class is used to call them, not the name of the object.

Since a static method refers to the class as a whole, and not to an object, we can NOT access non-static ones in it fields/properties and methods of the object, like the following:

    class Person {
        constructor(name, age) {
            this.name = name;
            this.age = 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(`Name: ${person.name}  Age: ${person.age}`);
        }

    }
    const alex = new Person("Alexander", 33);
    Person.print(alex);  // Alexander 33

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;

        }

        static calculateRestAges(person) {
            if (this.retirementAge > person.age) {//this.retirement Age - we refer to the static method inside the object

                const restAges = this.retirementAge - person.age;
                console.log(`left until retirement ${restAges} days`);

            }
        }
    }

    const alex = new Person("Alexander", 33);

    Person.calculateRestAges(alex); // 32 years until retirement

Private static fields and methods

Like normal fields and methods, static fields and methods can be private. Such properties and methods are only accessible from other static properties and methods of the class:

    class Person {

        static #retirementAge = 65;

        constructor(name, age) {
            this.name = name;
            this.age = age;
        }


        static calculateRestAges(person) {
            if (this.#retirementAge > person.age) {
                const restAges = this.#retirementAge - person.age;
                console.log(`left until retirement ${restAges} `);
            }

        }
    }
    console.log(Person.#retirementAge);  //Error: field retirementAge - private field

    const alex = new Person("Alexander", 33);
    Person.calculateRestAges(alex);      // left until retirement 32
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation