Subjects

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

How do getters and setters work in JavaScript classes? JavaScript classes में getters और setters कैसे काम करते हैं?

Answer

Getters and setters let you define methods that are accessed like properties, enabling computed properties and validation logic that runs transparently on read/write.

class Temperature {
    #celsius;

    constructor(celsius) {
        this.#celsius = celsius;
    }

    // Getter - accessed like a property, no parentheses needed
    get fahrenheit() {
        return (this.#celsius * 9) / 5 + 32;
    }

    // Setter - allows writing through validation logic
    set fahrenheit(value) {
        this.#celsius = ((value - 32) * 5) / 9;
    }

    get celsius() {
        return this.#celsius;
    }

    set celsius(value) {
        if (value < -273.15) {
            throw new Error('Temperature below absolute zero is impossible');
        }
        this.#celsius = value;
    }
}

const temp = new Temperature(25);
console.log(temp.fahrenheit); // 77 - called like a property, no ()

temp.fahrenheit = 100; // triggers the setter, converts and stores as celsius
console.log(temp.celsius); // 37.77...

try {
    temp.celsius = -300; // triggers validation in the setter
} catch (error) {
    console.log(error.message); // Temperature below absolute zero is impossible
}

// Getters/setters are also useful for computed properties
class Rectangle {
    constructor(width, height) {
        this.width = width;
        this.height = height;
    }
    get area() {
        return this.width * this.height; // always up to date, computed on access
    }
}
const rect = new Rectangle(5, 10);
console.log(rect.area); // 50
rect.width = 20;
console.log(rect.area); // 200 - recalculated automatically

Getters और setters ऐसे methods define करने देते हैं जो properties की तरह access होते हैं, computed properties और read/write पर transparently चलने वाली validation logic देते हैं।

class Temperature {
    #celsius;

    constructor(celsius) {
        this.#celsius = celsius;
    }

    // Getter - property की तरह access, parentheses नहीं चाहिए
    get fahrenheit() {
        return (this.#celsius * 9) / 5 + 32;
    }

    // Setter - validation logic के साथ write करने देता है
    set fahrenheit(value) {
        this.#celsius = ((value - 32) * 5) / 9;
    }

    get celsius() {
        return this.#celsius;
    }

    set celsius(value) {
        if (value < -273.15) {
            throw new Error('Temperature below absolute zero संभव नहीं');
        }
        this.#celsius = value;
    }
}

const temp = new Temperature(25);
console.log(temp.fahrenheit); // 77

temp.fahrenheit = 100; // setter trigger होता है
console.log(temp.celsius); // 37.77...

try {
    temp.celsius = -300;
} catch (error) {
    console.log(error.message);
}

// Computed properties के लिए भी उपयोगी
class Rectangle {
    constructor(width, height) {
        this.width = width;
        this.height = height;
    }
    get area() {
        return this.width * this.height;
    }
}
const rect = new Rectangle(5, 10);
console.log(rect.area); // 50
rect.width = 20;
console.log(rect.area); // 200 - automatic recalculate

Was this answer clear?