Constructor Functions

Updated · 7 min read
mediumES2026javascriptobjectsconstructornewlegacy

A constructor function is an ordinary function, by convention named with a capital letter, that is called with the new keyword to create and set up a new object. Before ES2015 introduced class, constructor functions were the standard way to make many objects of the same shape, and they still sit underneath every class today. This lesson shows them alongside the simpler factory function, so you can read older code and understand what class does behind the scenes.

In One Minute

  • A factory function is a plain function that returns a new object. No new, no this.
  • A constructor function is called with new: const a = new Account("Navika", 450);. Inside, this is the new object.
  • new creates an empty object, links it to Constructor.prototype, runs the function with this set to it, and returns it.
  • Put shared methods on Constructor.prototype so all objects use one copy.
  • Modern code uses class, which does the same thing with clearer syntax.

Factory Functions

The simplest way to create many similar objects is a function that returns one:

factory.js▶ Try it
function createAccount(owner, balance) {
  return {
    owner,
    balance,
    describe() {
      return `${this.owner} owes $${this.balance}`;
    },
  };
}
 
const navika = createAccount("Navika", 450);
const advitha = createAccount("Advitha", 45);
 
console.log(navika.describe());
console.log(advitha.describe());
console.log(navika.describe === advitha.describe);
Output
Navika owes $450
Advitha owes $45
false

Factory functions are easy to read and need no special keyword. One cost shows in the last line: each object gets its own copy of describe. With thousands of objects, that is thousands of identical functions in memory. Constructors and classes solve it with prototypes.

Constructor Functions and new

Syntax

syntax.js
function Name(parameters) {
  this.property = value;
}
 
const instance = new Name(arguments);

Example

constructor.js▶ Try it
function Account(owner, balance) {
  this.owner = owner;
  this.balance = balance;
}
 
Account.prototype.describe = function () {
  return `${this.owner} owes $${this.balance}`;
};
 
const navika = new Account("Navika", 450);
const advitha = new Account("Advitha", 45);
 
console.log(navika.describe());
console.log(advitha.describe());
console.log(navika.describe === advitha.describe);
console.log(navika instanceof Account);
Output
Navika owes $450
Advitha owes $45
true
true

Explanation: the constructor sets each object's own data (owner, balance) on this. The method is defined once on Account.prototype, and every object created with new Account can use it, so navika.describe === advitha.describe is true: one shared function. instanceof checks whether an object was made by a constructor.

What new Does

new Account("Navika", 450) performs four steps:

  1. Creates a new empty object.
  2. Links its prototype to Account.prototype, so the object can use the methods stored there.
  3. Calls Account with this set to the new object, and the given arguments.
  4. Returns the new object (unless the function explicitly returns a different object).

You can write the same steps by hand, which shows there is no magic:

new-by-hand.js▶ Try it
function Account(owner, balance) {
  this.owner = owner;
  this.balance = balance;
}
Account.prototype.describe = function () {
  return `${this.owner}: $${this.balance}`;
};
 
function fakeNew(Constructor, ...args) {
  const obj = Object.create(Constructor.prototype);   // steps 1 and 2
  const result = Constructor.apply(obj, args);         // step 3
  return result !== null && typeof result === "object" ? result : obj;   // step 4
}
 
const srihitha = fakeNew(Account, "Srihitha", 85);
console.log(srihitha.describe(), srihitha instanceof Account);
Output
Srihitha: $85 true

Object.create(proto) creates an object linked to proto, and apply calls a function with a chosen this; both have their own lessons later (prototypes, and call, apply and bind).

Forgetting new

Calling a constructor without new doesn't create an object. In strict mode (and in modules and classes), this is undefined and the first assignment throws:

forgot-new.js▶ Try it
"use strict";
function Account(owner) {
  this.owner = owner;
}
 
try {
  const broken = Account("Navika");
} catch (err) {
  console.log(err.name + ": " + err.message);
}
Output
TypeError: Cannot set properties of undefined (setting 'owner')

Without strict mode it's worse: this is the global object, so the function silently creates a global variable named owner and returns undefined. The capital letter convention exists to remind you to use new. Classes go further: calling a class without new always throws TypeError: Class constructor ... cannot be invoked without 'new'.

Constructor Functions vs Classes

The class below creates exactly the same kind of objects as the constructor function above. class is mostly a cleaner syntax for the same mechanism:

class.js▶ Try it
class Account {
  constructor(owner, balance) {
    this.owner = owner;
    this.balance = balance;
  }
  describe() {
    return `${this.owner} owes $${this.balance}`;
  }
}
 
const navika = new Account("Navika", 450);
console.log(navika.describe());
console.log(typeof Account, Object.hasOwn(Account.prototype, "describe"));
Output
Navika owes $450
function true

typeof Account is "function" for both: a class is a constructor function. Its methods end up on Account.prototype exactly as before. What classes add is safety and features: they must be called with new, their bodies are always strict, and they support extends, super, private #fields and static members cleanly. The prototypes and classes chapter covers all of that.

Factory functionConstructor functionClass
Created withcreateX()new X()new X()
Shared methodsNo (copies per object) unless added manuallyVia X.prototypeAutomatically
Forgetting newn/aSilent bug or TypeErrorAlways TypeError
Private dataClosuresClosures#private fields
Use todaySimple objects, especially with closuresReading older codeNew code

In Practice

Older code you maintain might define the Fee Tracker's payment type with a constructor function and built-in constructors such as Date. Recognizing the pattern lets you use and extend it safely, then migrate to a class when you touch it:

legacy.js▶ Try it
function Payment(name, amount, isoDate) {
  if (!new.target) throw new TypeError("Use new Payment(...)");
  this.name = name;
  this.amount = amount;
  this.date = new Date(isoDate + "T00:00:00");
}
 
Payment.prototype.label = function () {
  const date = this.date.toLocaleDateString("en-US");
  return `${date}  ${this.name}  $${this.amount.toFixed(2)}`;
};
 
const payments = [new Payment("Navika", 120, "2026-01-05"), new Payment("Advitha", 45, "2026-01-20")];
payments.forEach((p) => console.log(p.label()));
 
try {
  Payment("Srihitha", 85, "2026-02-01");
} catch (err) {
  console.log(err.message);
}
Output
1/5/2026  Navika  $120.00
1/20/2026  Advitha  $45.00
Use new Payment(...)

Common Mistakes

Forgetting new. Throws in strict mode, or creates globals in sloppy mode. Use new.target checks or a class.

Defining methods inside the constructor. this.describe = function () {...} creates a copy per object. Put methods on the prototype.

Replacing prototype entirely. Account.prototype = { describe() {...} } loses the constructor property and breaks objects created earlier. Add methods to the existing prototype.

Arrow functions as constructors or prototype methods. Arrows can't be called with new, and as prototype methods they don't get the object as this.

Practice

Challenge: Write a constructor function Student(name, grade) with a prototype method promote() that increases the grade by 1 (to a maximum of 12) and returns the object. Create two students and promote one twice.

Show solution
student.js▶ Try it
function Student(name, grade) {
  this.name = name;
  this.grade = grade;
}
 
Student.prototype.promote = function () {
  this.grade = Math.min(this.grade + 1, 12);
  return this;
};
 
const navika = new Student("Navika", 9);
const advitha = new Student("Advitha", 12);
navika.promote().promote();
advitha.promote();
 
console.log(navika.grade, advitha.grade);
console.log(navika.promote === advitha.promote);
Output
11 12
true

Quick check: Constructor functions

3 questions
1What does new do first?
2Where should shared methods of a constructor function go?
3What does typeof give for a class?

Key Takeaways

ConceptCode
Factoryfunction create(x) { return { x }; }
Constructorfunction Account(o) { this.owner = o; }
Shared methodAccount.prototype.describe = function () {...}
Createnew Account("Navika")
Guardif (!new.target) throw ...

Was this tutorial helpful?

Lesson 41 of 139