Constructor Functions
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, nothis. - A constructor function is called with
new:const a = new Account("Navika", 450);. Inside,thisis the new object. newcreates an empty object, links it toConstructor.prototype, runs the function withthisset to it, and returns it.- Put shared methods on
Constructor.prototypeso 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:
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);Navika owes $450
Advitha owes $45
falseFactory 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
function Name(parameters) {
this.property = value;
}
const instance = new Name(arguments);Example
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);Navika owes $450
Advitha owes $45
true
trueExplanation: 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:
- Creates a new empty object.
- Links its prototype to
Account.prototype, so the object can use the methods stored there. - Calls
Accountwiththisset to the new object, and the given arguments. - 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:
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);Srihitha: $85 trueObject.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:
"use strict";
function Account(owner) {
this.owner = owner;
}
try {
const broken = Account("Navika");
} catch (err) {
console.log(err.name + ": " + err.message);
}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 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"));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);
console.log(navika.describe());
console.log(typeof Account, Object.hasOwn(Account.prototype, "describe"));Navika owes $450
function truetypeof 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 function | Constructor function | Class | |
|---|---|---|---|
| Created with | createX() | new X() | new X() |
| Shared methods | No (copies per object) unless added manually | Via X.prototype | Automatically |
Forgetting new | n/a | Silent bug or TypeError | Always TypeError |
| Private data | Closures | Closures | #private fields |
| Use today | Simple objects, especially with closures | Reading older code | New 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:
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);
}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
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);11 12
trueKey Takeaways
| Concept | Code |
|---|---|
| Factory | function create(x) { return { x }; } |
| Constructor | function Account(o) { this.owner = o; } |
| Shared method | Account.prototype.describe = function () {...} |
| Create | new Account("Navika") |
| Guard | if (!new.target) throw ... |
Was this tutorial helpful?