Constructor Functions
Before the introduction of ES6 classes, Constructor Functions were the primary pattern for implementing Object-Oriented Programming (OOP) and code reuse in JavaScript. They act as blueprints for creating multiple objects of the same type.
1. Defining a Constructor
A constructor is a regular function, but by convention, it starts with a Capital Letter to signal that it should be invoked with the new keyword.
function User(name, email) {
this.name = name;
this.email = email;
}
const alice = new User("Alice", "alice@example.com");
2. Shared Logic via Prototypes
As discussed in Module 19, adding methods directly inside the constructor (using this.greet = ...) leads to memory inefficiency. The standard practice is to attach methods to the function's .prototype.
User.prototype.greet = function() {
console.log(`Hello, I'm ${this.name}`);
};
This ensures that only one copy of the function exists in memory, shared by all instances of User.
3. The new Keyword Mechanics
When you invoke a function with new, the engine performs four specific steps:
- It creates a new, empty object
{}. - It sets the object’s
__proto__to point to the function’s.prototype. - It calls the function with
thisbound to the new object. - It returns the object (unless the function returns another object).
4. Why use Constructors today?
While ES6 classes have replaced constructors in most modern applications, understanding them is vital because:
- Legacy Systems: Most enterprise JavaScript written before 2015 uses this pattern.
- Polyfills: Implementing modern features in older environments requires prototype-based constructors.
- Internals: Under the hood, Classes are just syntactic sugar over constructors.
Interview Pro-Tips: Instanceof vs Typeof
typeof: Returns"object"for both plain objects and instances of constructors. It’s useless for distinguishing between them.instanceof: Checks the prototype chain.alice instanceof Userwill betruebecausealicewas created by that constructor.
Technical Summary
Convention: Capitalization signals constructor intent.Efficiency: Always put methods on the prototype, not inside the constructor.New: The keyword that handles object creation, linking, and binding.