The 'new' Keyword and Constructor Calls
You'll learn how the new keyword creates a fresh object and automatically binds it as this inside a constructor function.
What Does 'new' Actually Do?
When you call a function with the new keyword, JavaScript performs some "magic" behind the scenes. It creates a brand-new empty object and sets this inside that function to point to this new object. Then, if the function doesn't explicitly return anything, JavaScript automatically returns that newly created object.
Think of it like an automated factory: new builds a container, hands it to your constructor function as this, lets your function customize it, then ships the finished product back to you.
The Four Steps of 'new'
When you write new MyConstructor(), JavaScript:
- Creates a brand-new empty object
- Binds
thisinsideMyConstructorto that new object - Executes the constructor function body
- Returns the new object (unless you explicitly return something else)
function Car(make, model) {
this.make = make; // 'this' is the new empty object
this.model = model; // we're adding properties to it
}
const myCar = new Car('Toyota', 'Camry');
console.log(myCar.make); // 'Toyota'
console.log(myCar.model); // 'Camry'
Without new, this would follow different binding rules (likely global or undefined in strict mode), and you wouldn't get a new object back.
Why This Matters
The new keyword gives us constructor binding — the fourth major rule for how this gets bound. It overrides the implicit and explicit binding rules you've learned, making this reliably point to the new instance being created.
Key Takeaway: The new keyword creates a fresh object, binds it as this inside the constructor function, and returns it automatically — giving you a reliable way to create object instances.