JavaScript Classes, Inheritance & Constructors

Master the blueprints of JavaScript to build organized, reusable, and powerful applications with Object-Oriented principles.

Lesson ProgressStep 1 of 8
/* Blueprints for Objects */
0 EXP

Welcome! Let's explore how to create object blueprints in JavaScript using classes.

/* Blueprints for Objects */

The `class` Keyword: A Blueprint

A `class` is a blueprint for creating objects. It encapsulates data (properties) and functions (methods) that operate on that data. Think of it as a cookie cutter: you define the shape once, then you can create many cookies (objects) from it.

class Vehicle {
  // ...
}

This simple declaration creates a new "type" called `Vehicle`. By convention, class names are capitalized.

System Check

What keyword is used to define a class blueprint in JavaScript?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🏆
Class Creator

Define a complete JavaScript class with a constructor.

🏗️
Order Expert

Correctly order the lines of a class definition.

✍️
Syntax Pro

Prove your mastery of inheritance syntax (`extends`, `super`).

Mission: Build a `Vehicle` Class

Create a `Vehicle` class with a `constructor` for `type`. Then, create a `Car` class that `extends` `Vehicle` and calls `super()` in its own constructor. Our AI assistant will provide real-time feedback.

A.D.A. Feedback:

> Awaiting input...

Challenge: Order the Class Definition

A class is defined in a logical order. Drag these lines into the correct sequence.

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

Challenge: Complete the Inheritance

Fill in the missing keywords to make `Dog` inherit from `Animal`.

class DogAnimal {(name, breed) {(name); ... }

Consult A.D.A.

Community Holo-Net

The Power of OOP: From Prototypes to Classes

When you first start with JavaScript, you primarily work with functions and objects. But as your applications grow, you need a way to organize your code, create reusable "blueprints," and manage complex relationships. This is where **Object-Oriented Programming (OOP)** comes in.

JavaScript has always been object-oriented, but its implementation was unique. It uses **prototypal inheritance**, where objects inherit directly from other objects. The ES6 (2015) `class` syntax didn't introduce a new OOP model; rather, it provided a much cleaner, more _syntactic sugar_ over this existing prototype system.

The Old Way: Prototype Chains

Before `class`, you would create a "constructor function" and add methods to its `prototype` property.

// 1. The constructor function
function Animal(name) {
  this.name = name;
}

// 2. Add methods to the prototype
Animal.prototype.speak = function() {
  console.log(this.name + ' makes a noise.');
}

// 3. Inheritance (The complicated part)
function Dog(name, breed) {
  // Call the parent constructor
  Animal.call(this, name);
  this.breed = breed;
}

// Link the prototypes
Dog.prototype = Object.create(Animal.prototype);
Dog.prototype.constructor = Dog;

// Add new method
Dog.prototype.speak = function() {
  console.log(this.name + ' barks.');
}

const d = new Dog('Fido', 'Lab');
d.speak(); // "Fido barks."

This was powerful but also verbose, confusing (especially `Object.create` and resetting the `constructor`), and prone to errors.

The New Way: `class` Syntax

The `class` keyword cleans this up immensely, making the code more readable and familiar to developers from other OOP languages like Java or Python.

// 1. The base class
class Animal {
  constructor(name) {
    this.name = name;
  }

  speak() {
    console.log(this.name + ' makes a noise.');
  }
}

// 2. Inheritance (Clean and simple)
class Dog extends Animal {
  constructor(name, breed) {
    // 3. Call parent constructor
    super(name);
    this.breed = breed;
  }

  // 4. Override method (Polymorphism)
  speak() {
    console.log(this.name + ' barks.');
  }
}

const d = new Dog('Fido', 'Lab');
d.speak(); // "Fido barks."

Core OOP Pillars in JavaScript

The `class` syntax makes it easier to implement the four main pillars of OOP:

  • Encapsulation: Bundling data (properties) and methods that operate on that data within one unit (the class). Modern JS even allows for true **private fields** using the `#` prefix, which hides data from outside the class.
    class Wallet {
      #balance = 0; // Private field
    
      deposit(amount) {
        this.#balance += amount;
      }
    }
  • Inheritance: Allowing a new class (child/subclass) to inherit properties and methods from an existing class (parent/superclass). This is done with `extends` and `super`.
  • Polymorphism: (Greek for "many forms") The ability for a child class to **override** a method from its parent. In our example, `Dog`'s `speak()` method overrides `Animal`'s `speak()` method.
  • Abstraction: Hiding complex implementation details and showing only the essential features. A `class` itself is a form of _abstraction_. We don't need to know *how* the `Dog` class works internally to use it.

Advanced Features: `static` and `get`/`set`

Static Methods

A `static` method is called on the class itself, not on an instance. It's often used for utility functions.

class MathUtil {
  static add(a, b) {
    return a + b;
  }
}
// Called on the class
const sum = MathUtil.add(2, 3);

Getters and Setters

`get` and `set` allow you to create properties that are backed by functions, useful for validation or computed values.

class User {
  constructor(first, last) {
    this.first = first;
    this.last = last;
  }
  get fullName() {
    return this.first + ' ' + this.last;
  }
}
const u = new User('John', 'Doe');
console.log(u.fullName); // "John Doe"
Key Takeaway: Always use `class` syntax in modern JavaScript for OOP. It simplifies the complex prototype system, makes your code more readable, and aligns JavaScript with standard OOP patterns, leading to more maintainable and scalable applications.

JavaScript OOP & Classes Glossary

class
A keyword that defines a template (blueprint) for creating objects. It's syntactic sugar over JavaScript's prototypal inheritance.
prototype
The underlying mechanism in JavaScript for inheritance. Every object has a prototype object it inherits properties and methods from.
constructor
A special method within a class, named `constructor`, that is automatically called when a new instance of the class is created. It's used to initialize the object's properties.
instance
A concrete object created from a class using the `new` keyword. For example, `const myCar = new Car()`. `myCar` is an instance.
new
An operator that creates a new instance of a class, automatically calling its `constructor`.
extends
A keyword used in a class declaration to create a child class that inherits from a parent class. Example: `class Dog extends Animal`.
super
A keyword used to call methods on a parent class. It is most commonly used as `super()` within a child's `constructor` to call the parent's `constructor`.
this
A keyword that refers to the current object instance. Inside a `constructor` or `method`, `this` is the object the method was called on.
method
A function that is defined inside a class and is a property of the class's instances.
static
A keyword that defines a method or property on the class itself, rather than on its instances. Called as `ClassName.staticMethod()`.
get / set
Keywords used to define "getters" and "setters" (accessor properties). These are functions that look like regular properties when accessed.
# (Private Field)
A prefix (`#`) used to declare private fields or methods within a class. These cannot be accessed from outside the class instance, enforcing encapsulation.
Encapsulation
The OOP principle of bundling data (properties) and the methods that operate on that data into a single unit (a class), and restricting direct access to some of the object's components (using private fields).
Polymorphism
The OOP principle that allows a child class to provide its own implementation of a method that is already defined by its parent class (method overriding).

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust applications with OOP principles.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ES2022+ specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!