#atom
ID: JS-011
Date: [Insert Date]

Content:
Classes in JavaScript are a syntactic sugar over the language's existing prototype-based inheritance. Introduced in ES6 (ECMAScript 2015), classes provide a cleaner and more familiar way to create objects and implement inheritance, especially for developers coming from class-based languages like Java or C++.


1. Definition of Classes


2. Syntax and Usage

Declaring a Class:

Use the class keyword to define a class.

Syntax:

class ClassName {
    // Constructor method (optional)
    constructor(parameters) {
        // Initialize properties
    }

    // Instance methods
    methodName() {
        // Method logic
    }

    // Static methods
    static staticMethodName() {
        // Static method logic
    }
}

Example:

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

    greet() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }

    static info() {
        console.log('This is a Person class.');
    }
}

Creating an Instance:

Use the new keyword to create an instance of a class.

Example:

const alice = new Person('Alice', 25);
alice.greet(); // "Hello, my name is Alice and I am 25 years old."

Static Methods:

Static methods belong to the class itself, not instances. They are called on the class, not on objects.

Example:

Person.info(); // "This is a Person class."

3. Inheritance

Classes support inheritance using the extends keyword. Child classes inherit properties and methods from parent classes.

Syntax:

class ChildClass extends ParentClass {
    constructor(parameters) {
        super(parameters); // Call the parent class constructor
        // Initialize child class properties
    }
}

Example:

class Student extends Person {
    constructor(name, age, major) {
        super(name, age); // Call the parent class constructor
        this.major = major;
    }

    study() {
        console.log(`${this.name} is studying ${this.major}.`);
    }
}

const bob = new Student('Bob', 20, 'Computer Science');
bob.greet(); // "Hello, my name is Bob and I am 20 years old."
bob.study(); // "Bob is studying Computer Science."

4. Getters and Setters

Classes support getter and setter methods for accessing and modifying properties.

Example:

class Rectangle {
    constructor(width, height) {
        this._width = width;
        this._height = height;
    }

    // Getter for area
    get area() {
        return this._width * this._height;
    }

    // Setter for width
    set width(value) {
        if (value > 0) {
            this._width = value;
        } else {
            console.log('Width must be positive.');
        }
    }
}

const rect = new Rectangle(10, 20);
console.log(rect.area); // 200
rect.width = 15; // Set width to 15
console.log(rect.area); // 300

5. Relationship Between Classes and Prototypes

Example:

class Animal {
    constructor(name) {
        this.name = name;
    }

    speak() {
        console.log(`${this.name} makes a sound.`);
    }
}

class Dog extends Animal {
    constructor(name, breed) {
        super(name);
        this.breed = breed;
    }

    speak() {
        console.log(`${this.name} barks.`);
    }
}

const dog = new Dog('Rex', 'German Shepherd');
dog.speak(); // "Rex barks."

6. Advantages of Classes


7. Limitations of Classes


Connections:


Connections:


Sources: