Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 8 of 10 · Object-Oriented JavaScript
Interview question

What is the difference between class fields and constructor-assigned properties? Class fields और constructor-assigned properties में क्या अंतर है?

Answer

Class fields (introduced in ES2022) let you declare instance properties directly in the class body, offering a more declarative alternative to assigning them inside the constructor.

// Constructor-assigned properties (traditional approach)
class UserOld {
    constructor(name) {
        this.name = name;
        this.createdAt = new Date();
        this.isActive = true;
    }
}

// Class fields (modern, declarative approach)
class User {
    createdAt = new Date(); // evaluated for EACH new instance
    isActive = true;         // default value, doesn't need constructor
    name; // declared without initial value

    constructor(name) {
        this.name = name; // still assigned in constructor since it varies per call
    }
}

const u1 = new User('John');
console.log(u1.isActive); // true - from class field default

// Class fields are especially useful for arrow function methods (bound 'this')
class Button {
    label = 'Click me';

    // Class field with arrow function - 'this' is bound to instance automatically
    handleClick = () => {
        console.log(`${this.label} was clicked`);
    };
}
const btn = new Button();
const handler = btn.handleClick;
handler(); // works correctly - 'this' remains bound, unlike a regular method

// Static class fields
class Config {
    static version = '1.0.0'; // class-level property, not per-instance
    static #instances = 0;    // private static field

    constructor() {
        Config.#instances++;
    }
    static getInstanceCount() {
        return Config.#instances;
    }
}
console.log(Config.version); // 1.0.0
new Config(); new Config();
console.log(Config.getInstanceCount()); // 2

Class fields (ES2022 में introduce) instance properties को class body में सीधे declare करने देते हैं, constructor के अंदर assign करने से ज़्यादा declarative alternative।

// पुराना approach - constructor में assign
class UserOld {
    constructor(name) {
        this.name = name;
        this.createdAt = new Date();
        this.isActive = true;
    }
}

// Modern approach - class fields
class User {
    createdAt = new Date(); // हर instance के लिए evaluate होता है
    isActive = true;
    name;

    constructor(name) {
        this.name = name;
    }
}

const u1 = new User('John');
console.log(u1.isActive); // true

// Arrow function methods के लिए class fields उपयोगी हैं ('this' bind)
class Button {
    label = 'Click me';

    handleClick = () => {
        console.log(`${this.label} was clicked`);
    };
}
const btn = new Button();
const handler = btn.handleClick;
handler(); // सही काम करता है, 'this' bound रहता है

// Static class fields
class Config {
    static version = '1.0.0';
    static #instances = 0;

    constructor() {
        Config.#instances++;
    }
    static getInstanceCount() {
        return Config.#instances;
    }
}
console.log(Config.version); // 1.0.0
new Config(); new Config();
console.log(Config.getInstanceCount()); // 2

Was this answer clear?