What they are: Special functions that create and set up new objects when called with new
Constructor Function Rules:
1. Naming convention: Start with capital letter
javascript
function User(name) { // ✅ Capital letter
this.name = name;
}
function createUser(name) { // ❌ Not a constructor naming
this.name = name;
}2. Must be called with new keyword
javascript
function User(name) {
this.name = name;
this.isAdmin = false;
}
const user1 = new User("John"); // ✅ Correct
const user2 = User("Jane"); // ❌ Wrong (this = undefined)What new Does Behind the Scenes:
javascript
function User(name) {
// Step 1: this = {} (new empty object created)
// Step 2: Add properties to this
this.name = name;
this.isAdmin = false;
// Step 3: return this (automatic)
}
// These are equivalent:
const user = new User("John");
const user = {
name: "John",
isAdmin: false
};Constructor vs Regular Function:
javascript
function User(name) {
this.name = name;
}
// Called as constructor
const user1 = new User("John");
console.log(user1.name); // "John"
// Called as regular function
const user2 = User("Jane");
console.log(user2); // undefined (function returns nothing)
console.log(window.name); // "Jane" (this = window in non-strict)Return Statement Rules:
1. Return object → returns that object
javascript
function User(name) {
this.name = name;
return { name: "Override" }; // Returns this instead of this
}
const user = new User("John");
console.log(user.name); // "Override"2. Return primitive → ignored, returns this
javascript
function User(name) {
this.name = name;
return "ignored"; // Primitive ignored
}
const user = new User("John");
console.log(user.name); // "John" (returns this)Adding Methods to Constructor:
javascript
function User(name, age) {
this.name = name;
this.age = age;
this.sayHi = function() {
console.log(`Hi, I'm ${this.name}`);
};
this.getInfo = function() {
return `${this.name} is ${this.age} years old`;
};
}
const user = new User("John", 30);
user.sayHi(); // "Hi, I'm John"
console.log(user.getInfo()); // "John is 30 years old"Interview Gotchas:
javascript
// Gotcha 1: Forgetting 'new'
function User(name) { this.name = name; }
const user = User("John"); // undefined, window.name = "John"
// Gotcha 2: Arrow functions can't be constructors
const User = (name) => { this.name = name; };
const user = new User("John"); // TypeError!
// Gotcha 3: Return object overrides
function User(name) {
this.name = name;
return {}; // Returns empty object, not {name: "John"}
}
// Gotcha 4: Each instance gets own method copy
function User(name) {
this.sayHi = function() {}; // New function for each instance!
}
// Better: Use prototypes for shared methodsConstructor vs Class (Preview):
javascript
// Constructor function
function User(name) {
this.name = name;
}
// ES6 Class (same thing, different syntax)
class User {
constructor(name) {
this.name = name;
}
}Bottom line: Constructor functions are factories for creating similar objects. Use capital letters, call with new, and this becomes the new object being created!
