Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 4 of 10 · Prototypes and Inheritance
Interview question

How do constructor functions and the 'new' keyword work together? Constructor functions और 'new' keyword साथ में कैसे काम करते हैं?

Answer

A constructor function is a regular function used with 'new' to create objects following a consistent template. The 'new' keyword performs several steps automatically.

function Person(name, age) {
    this.name = name;
    this.age = age;
}
Person.prototype.greet = function() {
    return `Hi, I'm ${this.name}, ${this.age} years old`;
};

const john = new Person('John', 30);
console.log(john.greet()); // Hi, I'm John, 30 years old

// What 'new Person(...)' actually does step by step:
function simulateNew(Constructor, ...args) {
    // 1. Create a new empty object with its prototype set to Constructor.prototype
    const obj = Object.create(Constructor.prototype);
    // 2. Call the constructor with 'this' bound to the new object
    const result = Constructor.apply(obj, args);
    // 3. If the constructor explicitly returns an object, use that instead
    return (typeof result === 'object' && result !== null) ? result : obj;
}
const simulated = simulateNew(Person, 'Jane', 25);
console.log(simulated.greet()); // Hi, I'm Jane, 25 years old

// Common mistake: forgetting 'new'
function PersonNoNew(name) {
    this.name = name; // 'this' is NOT bound to a new object in non-strict mode
}
const oops = PersonNoNew('Bob'); // called WITHOUT new
console.log(oops); // undefined - function returned nothing
// In strict mode or with classes, this throws a TypeError instead - safer

// ES6 classes enforce 'new' automatically
class PersonClass {
    constructor(name) {
        this.name = name;
    }
}
// PersonClass('John'); // TypeError: Class constructor cannot be invoked without 'new'

// Checking if a function was called with 'new'
function SafeConstructor(name) {
    if (!(this instanceof SafeConstructor)) {
        throw new Error('Must be called with new');
    }
    this.name = name;
}

Constructor function एक regular function है जो 'new' के साथ consistent template follow करके objects बनाने के लिए इस्तेमाल होती है। 'new' keyword automatically कई steps करता है।

function Person(name, age) {
    this.name = name;
    this.age = age;
}
Person.prototype.greet = function() {
    return `Hi, I'm ${this.name}, ${this.age} years old`;
};

const john = new Person('John', 30);
console.log(john.greet());

// 'new Person(...)' असल में क्या करता है step by step:
function simulateNew(Constructor, ...args) {
    const obj = Object.create(Constructor.prototype);
    const result = Constructor.apply(obj, args);
    return (typeof result === 'object' && result !== null) ? result : obj;
}

// गलती: 'new' भूलना
function PersonNoNew(name) {
    this.name = name; // गलत context में 'this'
}
const oops = PersonNoNew('Bob'); // बिना new के call
console.log(oops); // undefined

// ES6 classes automatically 'new' enforce करते हैं
class PersonClass {
    constructor(name) {
        this.name = name;
    }
}
// PersonClass('John'); // TypeError

Was this answer clear?