Lesson Overview

Greetings! Today, we're revisiting JavaScript classes, the core building block of Object-Oriented Programming (OOP) in JavaScript. Through hands-on examples, we'll revisit the fundamental concepts of JavaScript classes, including their structure, properties, and methods.

JavaScript Classes Refresher

Let's begin with a refresher on JavaScript classes. Essential to OOP, JavaScript classes bundle relevant data and functions into compact units called objects. Consider a video game character, which is a typical example of a class instance, with specific properties (such as health or strength) and methods (such as attack or defense).

class GameCharacter {
    // constructor method
    constructor(name, health, strength) {
        this.name = name;       // property
        this.health = health;   // property
        this.strength = strength;   // property
    }

    attack(otherCharacter) {    // method
        otherCharacter.health -= this.strength;
    }
}

JavaScript classes facilitate the grouping of associated code elements, simplifying their management. Now, to better remind ourselves how the above example works, let's go through it step-by-step.

Structure of a JavaScript Class

A JavaScript class serves as a blueprint consisting of properties and methods. While properties represent data relevant to a class instance, methods are actions or functions that manipulate this data. Each class includes a constructor function, which is used to define class properties.

An essential keyword within these methods is this, which represents the class instance. In object-oriented programming, it's needed to access the class's properties and methods. When a new class instance is created, JavaScript automatically passes it to the this parameter to access individual instance properties and methods using the this keyword. This mechanism allows each object to keep track of its own state and behaviors.

class GameCharacter {
    // constructor: defines class properties
    constructor(name, health, strength) {  
        this.name = name;       // property
        this.health = health;   // property
        this.strength = strength;   // property
    }
}

const character = new GameCharacter("Hero", 100, 20);  // object or instance of the class
Class Properties

Properties in JavaScript classes hold data associated with each class instance. For example, in our GameCharacter class, name, health, and strength are properties. You can access a class property using the object of the class, followed by a dot (.), and then the property name.

class GameCharacter {
    // constructor: defines class properties
    constructor(name, health, strength) {  
        this.name = name;       // property
        this.health = health;   // property
        this.strength = strength;   // property
    }
}

const character = new GameCharacter("Hero", 100, 20);  // object or instance of the class
console.log(character.name);  // prints: Hero
console.log(character.health);  // prints: 100
console.log(character.strength);  // prints: 20

Properties differentiate one class instance from another and store the state of the instance.

Class Methods

A class also contains methods — actions or functions that manipulate the data in the class. For example, the attack method in our GameCharacter class simulates an attack by one game character on another.

class GameCharacter {
    // constructor: defines class properties
    constructor(name, health, strength) {  
        this.name = name;       // property
        this.health = health;   // property
        this.strength = strength;   // property
    }

    attack(otherCharacter) {  // method
        otherCharacter.health -= this.strength;  // modifies 'otherCharacter's health property
    }
}

const character1 = new GameCharacter("Hero", 100, 20);   // First instance of GameCharacter
const character2 = new GameCharacter("Villain", 80, 15);  // Second instance

console.log(character2.health);  // prints: 80
character1.attack(character2);  // character1 attacks character2
console.log(character2.health);  // prints: 60
Examples of JavaScript Classes, Properties, and Methods
Lesson Summary and Practice

Great work revisiting JavaScript classes, their properties, and methods. JavaScript classes help organize your code, improving its readability and manageability. Now, test your understanding with exercise problems to solidify your newly refreshed knowledge. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal