Forgetting 'new': The Pitfall
You'll learn what goes wrong when you accidentally call a constructor function without the new keyword—and why it causes subtle bugs.
The Problem
When you call a constructor function without new, JavaScript treats it like a regular function call. This means:
- No new object is created — the automatic object creation step simply doesn't happen
thisbinds incorrectly — instead of pointing to a new object,thisfollows regular function rules (often binding to the global object orundefinedin strict mode)- Properties get attached to the wrong place — any properties you set on
thisend up somewhere unexpected
Seeing It In Action
function Car(make, model) {
this.make = make;
this.model = model;
}
// Correct usage with 'new'
const myCar = new Car('Toyota', 'Camry');
console.log(myCar.make); // 'Toyota' ✓
// Forgetting 'new' — disaster!
const brokenCar = Car('Honda', 'Civic');
console.log(brokenCar); // undefined (no object returned!)
console.log(typeof brokenCar); // 'undefined'
// Even worse: properties leaked to global scope
console.log(make); // 'Honda' (polluted global!)
console.log(model); // 'Civic'
Think of new as the "construction permit" for building an object. Without it, you're just running code that tries to modify this without creating a proper container for those properties.
In strict mode, this fails more obviously because this is undefined, causing an error when you try to set properties on it.
Key Takeaway: Always use new when calling constructor functions—forgetting it prevents object creation and causes this to bind incorrectly, leading to subtle bugs or global pollution.