JavaScript Classes Explained: Constructors, Methods, and Inheritance

Learn how JavaScript classes create objects, share methods through prototypes, and extend other classes without introducing a separate inheritance model.

JavaScript Classes Explained: Constructors, Methods, and Inheritance

What Are JavaScript Classes, and Are They a New Inheritance Model?

When several objects need the same kind of data and behavior, writing each object by hand can get repetitive. JavaScript classes provide a clear way to describe how to create those objects, including their initial data and shared methods.

Classes use JavaScript's existing prototype system for inheritance and shared methods. They don't introduce a separate inheritance model underneath. But class syntax has specific rules of its own, so it isn't a perfect one-to-one replacement for every older constructor-function pattern.

You can run the examples in a modern browser console or recent Node.js.

Create objects with a class

Use class to define the shape and behavior, and new to create an instance. The constructor runs when the instance is created:

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

  greet() {
    return `Hello, ${this.name}!`;
  }
}

const user = new User("Mina");
console.log(user.greet()); // Hello, Mina!

this.name is set on each new instance. The greet method is shared through User.prototype, rather than copied onto every instance:

const anotherUser = new User("Nafi");

console.log(Object.hasOwn(user, "name")); // true
console.log(Object.hasOwn(user, "greet")); // false
console.log(Object.getPrototypeOf(user) === User.prototype); // true

The instance owns its name value. It finds greet by looking up its prototype chain.

Extend a class

Use extends when one class should build on another. The child class can add behavior, and it can call the parent constructor with super():

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

  greet() {
    return `Hello, ${this.name}!`;
  }
}

class Admin extends User {
  constructor(name, section) {
    super(name);
    this.section = section;
  }

  review() {
    return `${this.name} reviews ${this.section}.`;
  }
}

const admin = new Admin("Mina", "tutorials");
console.log(admin.greet()); // Hello, Mina!
console.log(admin.review()); // Mina reviews tutorials.

Admin inherits greet through the prototype chain and defines its own review method. In a derived class constructor, call super() before using this.

What does “syntactic sugar” mean here?

Class syntax gives a familiar, readable way to work with constructors and prototypes. Class methods are placed on the class's prototype, and extends links prototype chains so instances can find inherited behavior.

For example, this check is true:

console.log(Object.getPrototypeOf(admin) === Admin.prototype); // true
console.log(Object.getPrototypeOf(Admin.prototype) === User.prototype); // true

So the inheritance still relies on prototypes. However, classes also have rules: they must be called with new, their bodies run in strict mode, and their methods are non-enumerable. It's more accurate to say class syntax is built on the prototype system, with its own semantics, rather than that every class is merely shorthand for a normal function.

Where classes fit in real projects

Classes can be useful when several instances share behavior and the class model makes that relationship clear—for example, in some Node.js services or domain models. JavaScript also supports factory functions and plain objects, which may fit a design better.

React supports class components, and you'll encounter them in older code. Modern React commonly uses function components and Hooks, so learning JavaScript classes is useful beyond React and doesn't mean every framework or app is internally class-based.

Common beginner mistakes

Forgetting new

Incorrect:

const user = User("Mina"); // TypeError: class constructor cannot be called without new

Correct: create an instance with new:

const user = new User("Mina");

Using this before super() in a child constructor

Incorrect:

class Admin extends User {
  constructor(name, section) {
    this.section = section; // Error: super() has not been called
    super(name);
  }
}

Correct: call super() first:

class Admin extends User {
  constructor(name, section) {
    super(name);
    this.section = section;
  }
}

Assuming methods are copied onto every instance

Instance data such as name belongs to each instance. Methods such as greet are shared through the prototype unless you explicitly create a per-instance function, for example with a class field arrow function.

Practice

Create a Product class with a constructor that stores a name and price, and a method that returns a short description. Then create a DigitalProduct class that extends it, calls super(), and adds a fileSize property. Check whether the methods are own properties or found on the prototype.

What we learned

  • A class describes how to create instances with data and shared behavior.
  • The constructor initializes each instance; methods normally live on the class prototype.
  • extends and super() build inheritance using prototype chains.
  • Classes use the prototype system but also have class-specific rules, so they aren't just identical to ordinary functions.
  • Classes are one JavaScript tool; plain objects and factory functions are also valid design choices.

Conclusion

JavaScript classes make object creation and inheritance easier to read, while still using prototypes underneath. Understanding that connection helps explain shared methods and inheritance behavior, whether you write classes directly or meet them in a codebase. Use classes when they make the model clearer, and choose another pattern when it fits better.

Written by

Dev Likhon

Dev Likhon

Full Stack Developer & Tech Educator

6+ years of professional development experience.

About Me
Share:

Comments

Share what you learned or ask a question.

Loading comments...