avatarKirti K

Summary

The provided web content is a comprehensive guide on JavaScript ES6 classes, focusing on private fields, static fields, and static methods, aimed at helping beginners prepare for JavaScript interviews as part of a 100-day learning journey.

Abstract

The web content titled "JavaScript Day 20— Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is the 20th installment in a series dedicated to JavaScript interview preparation. It delves into the advanced features of ES6 classes, including the syntax for defining private and static fields, as well as static methods. The article emphasizes the importance of understanding class syntax for data privacy and abstraction in JavaScript, which is a prototype-based language. It provides code examples to illustrate how to declare private fields using the hash (#) symbol, how to define static fields and methods, and how to access private static fields through public methods. The content also includes an HTML file example to demonstrate the practical application of these concepts and reiterates the significance of private fields in encapsulating data and preventing direct access or modification from external code. The author, Kirti Kaushal, encourages readers to proceed slowly with the concepts for better understanding and invites them to subscribe and follow for more insights, while promoting a culture of kindness and knowledge sharing in the coding community.

Opinions

  • The author believes that mastering JavaScript basics, particularly ES6 classes, is crucial for interview preparation.
  • Private fields are highlighted as essential for maintaining data privacy and abstraction in JavaScript programming.
  • The use of static fields and methods is presented as an important aspect of JavaScript classes that developers should be familiar with.
  • The article suggests that going slow and being thorough is a key approach to understanding classes in JavaScript.
  • There is an emphasis on the community aspect of coding, with encouragement to share knowledge and support each other.
  • The author expresses that kindness is an important trait in the coding community and that treating people with respect is valuable.
  • By providing practical examples and tips, the author conveys a commitment to making complex concepts more accessible to learners.

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

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

Welcome to Day 20–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 20 (April 11th, 2024) — ES6 Classes Part-2 Interview Questions ?

Note — Most important interview question

Question — Can we consider ES6 Classes as templates for creating objects ?

Answer — Yes indeed

Classes are a template for creating objects.

Tip : JavaScript is a protype-based language — an object’s behavior is specified by its own properties and its prototype’s properties.

How we can define classes — With class declarations

class MyCard{

}

// class declarations

Let’s now understanding different area’s of features of classes

class MyCard {
// how to define private field 
  #myLastName = "testmylastName"
  constructor(){ // This is our constructor
  
  }
// Instance field 
 email = "test.com" // defining a field

// how to define method /function
  getDetails(){
       // function body 
  }
 // how to define Static field 
   static myName = "test" // define with static keyword 
  // how to define Static Method 
   static myPersonalDetail(){
   }
   // how to define Static block 
   static {
   }
}

Let’s now break down each feature to understand it easily

Question 1 — What is private field and why we need it ?

Answer — As we are aware now, ES6 introduced new syntax for class, and it also provides new syntax for creating private properties.

As name suggest, private means privacy /personal and in programming terms which ensure privacy and allow us to hide implementation details and only expose the necessary functionality to the outside class.

It provides data privacy and abstraction.

How to declare private field ?

We can declare a private field by prefixing the field name with a hash (#) symbol.

Private field is only visible to the class to which they belong

#mylastName = "test"

How to Declare Static Field

class MyCard {
  #myLastName;

  constructor() {
    this.#myLastName = 'test';
  }
}

class MyDetail extends MyCard {
  #address;

  constructor() {
    super();
    this.#address = 'test road ';
  }
}

new MyDetail(); 

How to Access Private Static Field

class MyCard {
  #myLastName;

  constructor() {
    this.#myLastName = 'test';
  }
}

class MyDetail extends MyCard {
  #address;

  constructor() {
    super();
    this.#address = 'test road ';
  }
 // Public method to set the address 
    getAddress() {
        return this.#address;
    }
}


const myCar = new MyDetail();
console.log('test')
console.log(myCar.getAddress()) // We can access static by adding public method 

HTML File to TEST

<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Class Method</h1>

<p id="demo"></p>

<script>
class MyCard {
  #myLastName;

  constructor() {
    this.#myLastName = 'test';
  }
}

class MyDetail extends MyCard {
  #address;

  constructor() {
    super();
    this.#address = 'test road ';
  }
  getAddress() {
        return this.#address;
    }
}


const myCar = new MyDetail();
console.log('test')
console.log(myCar)
document.getElementById("demo").innerHTML=
"My address is " + myCar.getAddress();
</script>

</body>
</html>
JavaScript Day 20 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation — ES6 Classes Static HTML Result

Important Tip

We can’t access or modify directly static field and attempts to do will always return an error

// Error way 
console.log(myCar.#address)
myCar.#address = 'test'

// External code cannot access or modify the private property directly; 
//attempts to do so will result in an error.

// Error - caught SyntaxError: Private field '#address' must be declared 
// in an enclosing class 
JavaScript Day 20 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation

Tomorrow we will learn more about Static Private properties of ES6 with other examples.

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 :)

Cheers

Kirti Kaushal

Coding Interviews
Javascript Tips
Javascript Development
Front End Development
Programming
Recommended from ReadMedium