By Rahul — Google Frontend Engineer
The 4 Steps of new
When you write new Foo(), JavaScript does exactly four things:
- Creates a new empty object
{} - Sets the prototype of that object to
Foo.prototype - Calls
Foo()withthisbound to the new object - If
Fooreturns an object, use that. Otherwise return the new object from step 1
Implementing new Yourself
The Return Value Trap
Forgetting new
new with Arrow Functions
Best Practices
- Use ES6 classes instead of constructor functions — they enforce
newautomatically - If you must use constructor functions, add the
instanceofcheck - Capitalize constructor function names (convention that signals "use new")
- Never return objects from constructors unless you have a specific reason
Summary
The new operator creates an object, links its prototype, calls the constructor, and handles the return value. Understanding these 4 steps is fundamental to understanding JavaScript's object model.