avatarKirti K

Summary

The provided web content is a comprehensive guide on mastering JavaScript basics, specifically focusing on ES6 classes, as part of a 100-day interview preparation journey for JavaScript beginners.

Abstract

The web content titled "JavaScript Day 22 — Mastering JavaScript Basics: A Comprehensive Guide for Interview Preparation" is the 22nd installment in a series aimed at JavaScript interview preparation for beginners. It delves into ES6 classes, covering class declarations and expressions, the structure of class bodies, and the concept of class elements, including fields, properties, and methods. The article emphasizes understanding static methods and properties, providing examples and explaining how to access them. The content is part of a larger initiative called "100 Days of JavaScript Interview Prep: A Beginner’s Journey," which encourages learning, practice, and growth in JavaScript through a structured 100-day plan.

Opinions

  • The author stresses the importance of understanding ES6 classes for interview preparation.
  • Classes are presented as a fundamental concept in JavaScript, akin to special functions, which can be declared or expressed.
  • The article suggests that a solid grasp of class elements, including their kind, location, and visibility, is crucial for mastering JavaScript basics.
  • Static methods and properties are highlighted as valuable features for creating utility functions and managing shared data across instances.
  • The author encourages a slow and steady approach to fully comprehending classes, implying that this concept is pivotal and warrants careful study.
  • The content is positioned as a supportive resource, with the author inviting readers to subscribe, clap, and follow for more insights, indicating a commitment to shared knowledge and community support.

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

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

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

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

Note — Most important interview question

Question 1 — How to define ES6 Class Declaration and Expression with example?

Answer :

As per our Part 1, Part 2 and Part 3 article’s we can now relate Classes in nothing but special function’s

We can also define classes as function declaration and function expression.

If you need to under what is declaration and function expression, please consider our Day 11 (Please find below link for brush-up)

Let’s now understand ES6 Class w.r.t to declaration and expression

Declaration

// Declaration 
class Mycard {
  constructor(name, address) {
    this.name = name;
    this.address = address;
  }
}

Expression

1) Class is anonymous but assigned to a variable.

const MyCard = class {
  constructor(name, addres) {
    this.name = name;
    this.address = address;
  }
};

2) Class with name and assigned to MyCard variable

// Expression; the class has its own name
const MyCard = class Card {
 constructor(name, addres) {
    this.name = name;
    this.address = address;
  }
};

Question 2 — What do you understand by class body ?

Answer —

Class body is the part which is covered by {} and it refers to the block of code which defines the behavior and structure of a class.

It defines the entire definition and implementation.

We define methods, constructors and other fields inside class body.

class MyCard {
// start of body 
  static #address = 'test address'; // static field. 
   getDetail(){ // normal method
     return "this is my normal function whichtance"
   }
// end of body
}

Question 3 — What do you understand by Class Element ?

Answer : Class elements means fields or properties and methods.

Class Elements can be explained as below (K L and V)

K refers to Kind, L refer to location and V refers to Visibility

Kind: Getter, setter, method, or field

Location: Static or instance

Visibility: Public or private

Let’s understand with some examples below

class MyCard {
  constructor(firstName, address) {
    this.firstName = firstName;
    this.address = address;
  }
  // Getter 
  get details() {
    return this.myDetails();
  }
  // Method
  myDetails() {
    return `My Name is ${this.firstName} and my address is ${this.address}`;
  }
 
}

const personalDetail= new MyCard('kirti', 'test address');

console.log(personalDetail.details); 

Question 4— How we can define static method and how we can access it ?

Answer -

To define static method or properties, we need to use static a keyboard.

Static properties (fields and methods) are defined on the class itself instead of each instance.

Usage -

To create utility functions or consider it as helper classes

It also helps us to create caches, fixed configuration or data which doesn’t need to replication.

Please find below example with output.

class MyCard {
    // Static variable
    static bedroom= 0;

    // Static method
    static addBedroom() {
        MyCard.bedroom++;
    }

    // Static method accessing static variable
    static getBedroomDetails() {
        return MyCard.bedroom;
    }
}

// Accessing static variable directly
console.log("Intial Value of bedroom:", MyCard.bedroom);  

// Incrementing static variable using static method
MyCard.addBedroom();
console.log("Modified BedRoom:", MyCard.bedroom);  


// Accessing static variable using static method
console.log("Our Method :", MyCard.getBedroomDetails());  

We will still continue with our classes in our tomorrow blog.

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

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