01 / 20

Constructor
Functions

Learn how JavaScript uses constructor functions to create multiple objects with shared structure: the foundation of object-oriented patterns in JS.

JavaScript

What Is a JavaScript Object?

An object is a collection of related data and functionality bundled together. Each piece of data is stored as a property (a key-value pair), and each behavior is stored as a method (a function inside the object).

const dog = { name: "Buddy", breed: "Golden Retriever", age: 3, bark() { return "Woof!"; } };

The Problem with Object Literals

If you need many objects with the same structure, writing each one by hand is repetitive, error-prone, and violates the DRY principle (Don't Repeat Yourself).

dog1 name: "Buddy" age: 3 bark() dog2 name: "Rex" age: 5 bark() dog3 name: "Max" age: 2 bark() Same structure repeated 3 times! Imagine 100 dogs... ✕

What Is a Constructor Function?

A constructor function is a regular JavaScript function that serves as a blueprint for creating objects. When called with the new keyword, it automatically creates a new object, sets up its properties and methods, and returns it.

Blueprint
Defines the structure every object will have: which properties and methods.
new Keyword
Tells JavaScript to create a fresh object and run the constructor on it.
this Keyword
Inside the constructor, this refers to the brand-new object being created.

The Core Pattern

// 1. Define the constructor (capitalized name) function Dog(name, breed, age) { this.name = name; this.breed = breed; this.age = age; this.bark = function() { return this.name + " says Woof!"; }; } // 2. Create instances with "new" const buddy = new Dog("Buddy", "Golden Retriever", 3); const rex = new Dog("Rex", "German Shepherd", 5);

What new Does Behind the Scenes

When you write new Dog("Buddy", ...), JavaScript performs four automatic steps:

1
Creates a new empty object: JavaScript creates {} behind the scenes.
2
Sets this to that new object: Inside the function, this now points to the fresh object.
3
Runs the function body: The property assignments like this.name = name attach data to the new object.
4
Returns the new object: You don't need to write return: it happens automatically.

Blueprint → Instances

Constructor: Dog() this.name = name this.breed = breed this.age = age this.bark = function() new new new buddy name: "Buddy" breed: "Golden" age: 3 bark() rex name: "Rex" breed: "Shepherd" age: 5 bark() max name: "Max" breed: "Poodle" age: 2 bark()

Constructor vs. Object Literal

Constructor Function Object Literal
Purpose Create many similar objects Create one specific object
Reusability ✓ Highly reusable ✕ Single use
Syntax function + new { key: value }
Naming PascalCase (Dog) camelCase (myDog)
Best for Entities, models, templates Config, options, one-offs

How to Build a Constructor

1
Choose a name: Use PascalCase (e.g., Car, Student, Product).
2
Define parameters: List the data each instance will need (name, age, etc.).
3
Assign properties with this: Write this.propertyName = parameterValue for each.
4
Add methods with this: Attach functions that use this to access instance data.
5
Create instances: Call with new ConstructorName(...).

Example 1: Car Constructor

function Car(make, model, year) { this.make = make; this.model = model; this.year = year; this.describe = function() { return this.year + " " + this.make + " " + this.model; }; } const myCar = new Car("Toyota", "Camry", 2024); console.log(myCar.describe()); // "2024 Toyota Camry" console.log(myCar.make); // "Toyota"

Example 2: Student Constructor

function Student(name, grade, gpa) { this.name = name; this.grade = grade; this.gpa = gpa; this.isHonorRoll = function() { return this.gpa >= 3.5; }; } const alice = new Student("Alice", 10, 3.8); const bob = new Student("Bob", 11, 2.9); console.log(alice.isHonorRoll()); // true console.log(bob.isHonorRoll()); // false

Example 3: Rectangle with Methods

function Rectangle(width, height) { this.width = width; this.height = height; this.area = function() { return this.width * this.height; }; this.perimeter = function() { return 2 * (this.width + this.height); }; } const r1 = new Rectangle(5, 10); console.log(r1.area()); // 50 console.log(r1.perimeter()); // 30

Checking Type with instanceof

JavaScript remembers which constructor created each object. The instanceof operator lets you verify an object's type:

const buddy = new Dog("Buddy", "Golden", 3); const myCar = new Car("Toyota", "Camry", 2024); console.log(buddy instanceof Dog); // true console.log(buddy instanceof Car); // false console.log(myCar instanceof Car); // true

Common Mistakes

✕ Forgetting "new"
Calling Dog("Buddy") without new makes this point to the global object, not a new instance. Properties leak into global scope.
✕ Lowercase Name
Using function dog() instead of function Dog() doesn't cause an error, but breaks convention and confuses other developers.
✕ Missing "this"
Writing name = name instead of this.name = name creates a local variable. The property never gets attached to the object.

Key Concepts Summary

Concept What It Does Example
Constructor Blueprint for objects function Dog() {}
new Creates a fresh instance new Dog("Rex")
this Refers to current instance this.name = name
instanceof Checks object's constructor rex instanceof Dog
PascalCase Naming convention Car, Student, Dog

Question 1 of 5

What is the primary purpose of a constructor function in JavaScript?

Question 2 of 5

What does the this keyword refer to inside a constructor function called with new?

Question 3 of 5

What is the value of myBook?

Question 4 of 5

Which naming convention should you follow for constructor functions in JavaScript?

Question 5 of 5

What is the result of c1.area() + c2.area()?