Constructor
Functions
Learn how JavaScript uses constructor functions to create multiple objects with shared structure: the foundation of object-oriented patterns in JS.
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).
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).
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.
this refers to the brand-new object being created.The Core Pattern
What new Does Behind the Scenes
When you write new Dog("Buddy", ...), JavaScript performs four automatic steps:
{} behind the scenes.this to that new object: Inside the function, this now points to the fresh object.this.name = name attach data to the new object.return: it happens automatically.Blueprint → Instances
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
PascalCase (e.g., Car, Student, Product).this: Write this.propertyName = parameterValue for each.this: Attach functions that use this to access instance data.new ConstructorName(...).Example 1: Car Constructor
Example 2: Student Constructor
Example 3: Rectangle with Methods
Checking Type with instanceof
JavaScript remembers which constructor created each object. The instanceof operator lets you verify an object's type:
Common Mistakes
Dog("Buddy") without new makes this point to the global object, not a new instance. Properties leak into global scope.function dog() instead of function Dog() doesn't cause an error, but breaks convention and confuses other developers.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?
new to stamp out each instance.Question 2 of 5
What does the this keyword refer to inside a constructor function called with new?
new, JavaScript creates a fresh empty object and sets this to point to it. Every this.property = value assignment attaches data to that new object.Question 3 of 5
Consider the following code:
function Book(title, author) { this.title = title; this.author = author; this.summary = function() { return this.title + " by " + this.author; }; } const myBook = Book("1984", "Orwell");
What is the value of myBook?
new keyword is missing! Without new, the function runs as a regular function. Since it has no return statement, it returns undefined. The properties get assigned to the global object instead of a new instance.Question 4 of 5
Which naming convention should you follow for constructor functions in JavaScript?
Dog, Car, Student. This signals to other developers that the function should be called with new.Question 5 of 5
Consider the following code:
function Circle(radius) { this.radius = radius; this.area = function() { return Math.PI * this.radius * this.radius; }; } const c1 = new Circle(5); const c2 = new Circle(10);
What is the result of c1.area() + c2.area()?
c1.area() = \( \pi \times 5^2 \approx 78.54 \). c2.area() = \( \pi \times 10^2 \approx 314.16 \). The sum is approximately 392.7. Each instance computes its own area using its own radius value.