avatarKirti K

Summary

This context provides an explanation and examples of creating private fields and methods in ES6 classes, as well as static private fields and methods.

Abstract

The content of this context is part of a 100-day JavaScript interview preparation journey, focusing on ES6 classes. The author explains how to create private fields and methods in ES6 classes using the # prefix and provides examples to illustrate their usage. The author also discusses static private fields and methods, which are similar to normal static classes but with the added advantage of privacy. The examples demonstrate how to create and use static private fields and methods, as well as how to access them from outside the class.

Bullet points

  • Private fields and methods in ES6 classes can be created using the # prefix.
  • Private fields and methods can only be accessed within the class that declared them.
  • Attempting to access or modify private fields and methods from outside the class will result in an error.
  • Static private fields and methods are similar to normal static classes but with the added advantage of privacy.
  • Static private fields and methods can be accessed and modified using the class name and the # prefix.
  • Attempting to access or modify static private fields and methods from outside the class will also result in an error.
  • Examples are provided to illustrate the usage of private fields and methods, as well as static private fields and methods.

JavaScript Day 23 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

100 Days of JavaScript Interview Prep: A Beginner’s Journey

Welcome to Day 23–100 Days of JavaScript Interview Prep: A Beginner’s Journey

Embarking on a 100-day adventure focused on learning, practice, and growth sounds like an incredibly rewarding journey.

DAY 23 (April 14th, 2024) — ES6 Classes Part 5 - Interview Questions ?

Note — Most important interview question

Question 1— How to create ES6 Class with private field and private method ?

Answer :

ES6 introduced private field and private field can be declared with help of # pre-fix.

Note : As the name specify, private can access only within the class itself who declared or define it. It means can’t access or modified from outside the class

Let’s now understand it with basic example

class MyCard {
    #address; // Private property

    constructor(address) {
        this.#address = address; // Assigning a value to the private property
    }

    // Public method to get the address
    getAddress() {
        return this.#address;
    }

    // Public method to set the address
    setAddress(address) {
        this.#address = address;
    }
}

const card = new MyCard('123 road street');

console.log(card.getAddress()); // Output: 123 road street'

// Using the public method to set the address
card.setAddress('new roaod 1223'); 

console.log(card.getAddress()); 
// Output: new roaod 1223
Private ES6 Method and Field outPut

b) First Error Use Case

Trying to access the private property directly will result in an error

class MyCard {
    #address; // Private property

    constructor(address) {
        this.#address = address; 
        // Assigning a value to the private property
    }

    // Public method to get the address
    getAddress() {
        return this.#address;
    }

    // Public method to set the address
    setAddress(address) {
        this.#address = address;
    }
}

const card = new MyCard('123 road street');

// Trying to access the private property directly will result in an error
 console.log(card.#address); 
// it will give error in terminal 

Trying to access the private property directly will result in an error

c) Second Error, Use Case

Trying to set the private property directly will also result in an error

class MyCard {
    #address; // Private property

    constructor(address) {
        this.#address = address; 
        // Assigning a value to the private property
    }

    // Public method to get the address
    getAddress() {
        return this.#address;
    }

    // Public method to set the address
    setAddress(address) {
        this.#address = address;
    }
}

const card = new MyCard('123 road street');


// Trying to set the private property directly will also result in an error
 card.#address = 'new road 1234 street'; 
// it will give error in terminal 
//SyntaxError: Private field '#address' must be declared in an enclosing class
Trying to set the private property directly will also result in an error — output

Question 2 — What do you understand by Static Private ES6 method and field? Please explain with example

Answer —

ES6 introduced Static Private, and it is similar to normal Static class with advantage of Private.

Let’s see how it works with simple example :)

As we also aware now that private always start with # and if we need to create Static — we need to add prefix Static

Which means when we need static + Private == static #fieldname or static #methodname()

To learn more about Static Or private basic foundation, please follow our below Day 20, 21 and 22 for understanding of Static, Private ES6 Classes in theoretical.

Today we are focusing on few examples to understand the combination of both static + private.

That’s why the reason — you will notice the combination of static #fieldname or static #methodname

Note — Accessing directly static private field and method will return error.

Let’s Start with few examples to understand the concept of Static , Private and Static private

Enjoy Coding with Examples

private static field denoted by the # prefix.

class MyCard {
    // Private static field
    static #address = 'test 123';

    // Public static method to get the address
    static getAddress() {
        return MyCard.#address;
    }

    // Public static method to change  the addres
    static changeAddress(newAddress) {
        MyCard.#address = newAddress;
    }
}

// Accessing static private field and method
console.log(MyCard.getAddress()); // Output: test 123
MyCard.changeAddress('212 address');
console.log(MyCard.getAddress()); // Output: 212 address
MyCard.changeAddress('113 addres');
console.log(MyCard.getAddress()); // Output: 113 addres

Method 2 — How to Create Private Static Method

class MyCard {
    // Private static field
    static #address = 'test 123';
    // Public static method to get the address
    static getAddress() {
        return MyCard.#address;
    }
    // Public static method to change  the addres
    static changeAddress(newAddress) {
        MyCard.#address = newAddress;
    }
}

// Accessing static private field and method
// Trying to access the private static field directly will result in an error
 console.log(MyCard.#address); 
 // This would throw an error
// Trying to modify the private static field directly will also result in an error
 MyCard.#address = 10; // This would throw an error

Error Use Cases

Accessing static private field and method result an error :)

class MyCard {
    // Private static field
    static #address = 'test 123';
    static #lastName;

    // Public static method to get the address
    static getAddress() {
        return MyCard.#address;
    }

    // Public static method to change  the addres
    static changeAddress(newAddress) {
        MyCard.#address = newAddress;
    }

    static #changeLastName(lastName) {
        MyCard.#lastName = lastName;
    }

    static updateLastName(lastName) {
        MyCard.#changeLastName(lastName);
    }

    static getLastName() {
        return MyCard.#lastName;
    }
}


// Accessing static private field and method

// Trying to access the private static field directly will result in an error
 //console.log(MyCard.#address); 
 console.log(MyCard.#lastName); 
 // This would throw an error

// Trying to modify the private static field directly will also result in an error
 //MyCard.#address = 10; // This would throw an error
Accessing static private field and method result an error :)

Accessing Method

class MyCard {
    // Private static field
    static #address = 'test 123';
    static #lastName;

    // Public static method to get the address
    static getAddress() {
        return MyCard.#address;
    }

    // Public static method to change  the addres
    static changeAddress(newAddress) {
        MyCard.#address = newAddress;
    }

    static #changeLastName(lastName) {
        MyCard.#lastName = lastName;
    }

    static updateLastName(lastName) {
        MyCard.#changeLastName(lastName);
    }

    static getLastName() {
        return MyCard.#lastName;
    }
}


// Accessing static private field and method

// Trying to access the private static field directly will result in an error
 //console.log(MyCard.#address); 
 //console.log(MyCard.#lastName); 
 // This would throw an error

 // Attempting to access the private static method directly will result in an error
 MyCard.#changeLastName('hgfhfghf new'); 
// This would throw an error
Accessing static private field and method result an error :)

Go slow always with Classes Concept — :)

Happy Coding and Stay Tuned :)

Kind is cool !!!

Share knowledge to gain knowledge !!!!

Be kind and treat people with respect !!

If you find my blog helpful, then please subscribe, claps and follow to support :)

Frontend Interviews
Coding Interviews
Frontend Development
Javascript Interview
Programming
Recommended from ReadMedium