Private properties and methods of a JavaScript class. Code e... open

Private properties and methods of a JavaScript class. Code example

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

The usual properties and methods of the class were available from the outside, and accordingly we could refer to them anywhere in the program.

For example:

    class Person {

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

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

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

    alex.name = "Bob";//we can override the class property
    alex.age = 185;//sometimes this can have bad consequences when running a program
    alex.print();   // Name: Bob  Age: 185

But what if we do not want the program to be able to change properties and methods only inside the object? Or in other words, make the properties and methods of the class private – available only for this class.

The JavaScript language provides the necessary tools for this. To do this, the names of fields and methods must begin with the pound sign #.

Private Object properties


    class Person {
        #name;//let's create the private properties of the object
        #age;

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

        //now to operate these properties it is necessary to add #
        print() {
            console.log(`Name: ${this.#name}  Age: ${this.#age}`);
        }
    }

    const tom = new Person("Tom", 37);
    tom.#name = "Sam";//Error - private field
    tom.#age = -45;//Error - private field

In the example above, the private fields #name and #age are defined. You can set and get their value only inside the class Person. Outside of it they are not available.

Accessing private properties and methods of an Object

If you need to somehow access them, then we can define methods for this:

    class Person {
        #name;
        #age = 1;

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

        //let's write a setter method that will allow us to set the value of the #age property, as well as perform certain checks
        setAge(age) {
            if (age > 0 && age < 110) this.#age = age;
        }

        print() {
            console.log(`Name: ${this.#name}  Age: ${this.#age}`);
        }
    }
    const tom = new Person("Alexander", 33);
    tom.print();    // Name: Alexander  Age: 33

    tom.setAge(22); //let's set a new age through a function setAge

    tom.print();    // Name: Alexander Age: 22

Private Object Methods

As a rule, private methods are used to perform some auxiliary actions, such as validation, and
which it makes no sense to make accessible from outside.

Let’s create a private method, also using #:

    class Person {
        #name = "undefined";
        #age = 1;

        constructor(name, age) {
            this.#name = this.#checkName(name);
            this.setAge(age);
        }
        #checkName(name) {
            if (name !== "admin") return name;
        }

    }

In the example above, a private method #checkName() is defined, which performs a conditional check of the name – if it is not equal to “admin” then returns the passed value. And also outside the class, we cannot refer to this method:

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