TypeScript

What is the difference between a prototype and a constructor in TypeScript?

These two concepts get confused constantly. They do different things.

24 Apr 2024

What is the difference between a prototype and a constructor in TypeScript?

These two concepts get confused constantly. They do different things.

Constructor: builds the object

A constructor runs when you create a new instance with new. It sets up the object's own properties — the data that belongs to this specific instance.

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

const person = new Person('John');
console.log(person.name); // "John"

Each Person instance gets its own name. That's the constructor's job.

Prototype: shares behavior

A prototype is an object that other objects inherit from. Methods defined on the prototype are shared across all instances — they exist once in memory, not once per instance.

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

Person.prototype.sayHello = function() {
  console.log(`Hello, my name is ${this.name}!`);
};

const john = new Person('John');
john.sayHello(); // "Hello, my name is John!"

sayHello lives on Person.prototype, not on john directly. Every Person instance shares the same function. When you call john.sayHello(), JavaScript walks up the prototype chain and finds it.

The key difference

Constructor = sets up instance-specific data (runs once per new).

Prototype = defines shared behavior (exists once, inherited by all instances).

When you write a method inside a class body, JavaScript puts it on the prototype automatically. The class syntax hides the prototype mechanics, but they're still there.

The trade-off

Prototypes save memory — one function shared across thousands of instances. But prototype chain lookups are slightly slower than own-property access. For most applications, this is irrelevant. For performance-critical code with millions of objects, it might matter.

Understanding this distinction matters because JavaScript's object model is prototype-based, not class-based. The class keyword is syntax sugar. Under the hood, it's still prototypes all the way down.

Keep reading