JS Object Get/Set
In this page:
const objectName = {
get propertyName() {
return value;
},
set propertyName(newValue) {
// assign
}
};
Getter परिभाषित करना
Getter, जो object literal या class के भीतर get propertyName() { ... } के रूप में लिखा जाता है, अपनी function body को अपने-आप तब चलाता है जब वह property पढ़ी जाती है -- बाहर से, यह ठीक साधारण property की तरह दिखता और व्यवहार करता है, उसे call करने के लिए कोष्ठकों की ज़रूरत नहीं।
उदाहरण: Defining a Getter
const circle = {
radius: 5,
get area() { return Math.PI * this.radius ** 2; },
};
console.log(circle.area); // no parentheses, looks like a property
Setter परिभाषित करना
Setter, जो set propertyName(value) { ... } के रूप में लिखा जाता है, अपनी function body को अपने-आप तब चलाता है जब वह property assign की जाती है -- जो आपको assign किए गए value को validate, transform या redirect करने देता है इससे पहले कि आप तय करें कि उसे असल में कैसे (या क्या) store करना है।
उदाहरण: Defining a Setter
// Declare the constant `user`; its value is built below
// Declare the constant `user`; its value is built below
const user = {
_age: 0,
// Define the setter `age` taking `value`
// Define the setter `age` taking `value`
set age(value) {
if (value < 0) throw new Error("Age cannot be negative");
// Assign `value` to `this._age`
// Assign `value` to `this._age`
this._age = value;
},
};
// Assign `25` to `user.age`
// Assign `25` to `user.age`
user.age = 25;
// Print `user._age` to the console
// Print `user._age` to the console
console.log(user._age);
एक ही Property के लिए Getter और Setter को मिलाना
एक ही property नाम के लिए get और set दोनों परिभाषित करना उसे पूरी तरह कस्टम, validated, computed property की तरह व्यवहार करने देता है -- बाहर से ठीक साधारण property की तरह पढ़ने और लिखने योग्य, जबकि दोनों दिशाओं में पर्दे के पीछे आपका अपना logic चलता है।
उदाहरण: Combining a Getter and Setter for the Same Property
// Declare the constant `temperature`; its value is built below
// Declare the constant `temperature`; its value is built below
const temperature = {
_celsius: 0,
get fahrenheit() { return this._celsius * 9 / 5 + 32; },
set fahrenheit(value) { this._celsius = (value - 32) * 5 / 9; },
};
// Assign `100` to `temperature.fahrenheit`
// Assign `100` to `temperature.fahrenheit`
temperature.fahrenheit = 100;
// Print `temperature._celsius` to the console
// Print `temperature._celsius` to the console
console.log(temperature._celsius);
Classes में Getters और Setters
Class के भीतर, get और set methods ठीक वैसे ही काम करते हैं जैसे object literal में -- class पर एक बार परिभाषित होकर, वे अपने-आप हर instance पर लागू होते हैं, जिससे उस class के हर object को हर instance के लिए logic दोहराए बिना एकसमान computed या validated property व्यवहार मिलता है।
उदाहरण: Getters and Setters in Classes
// Define the class `Circle`
// Define the class `Circle`
class Circle {
constructor(radius) { this.radius = radius; }
get area() { return Math.PI * this.radius ** 2; }
}
// Print `new Circle(3).area` to the console
// Print `new Circle(3).area` to the console
console.log(new Circle(3).area);
Getters/Setters बनाम साधारण Properties कब
जब किसी गणना या validation की ज़रूरत न हो तब साधारण property ज़्यादा सरल और पर्याप्त है -- getter/setter पर विशेष रूप से तब जाइए जब property को अन्य data से निकाला जाना हो, या हर assignment पर कोई नियम लागू करना हो, उस logic को एक जगह रखते हुए बजाय इसके कि वह हर उस जगह बिखरा हो जहाँ value सेट की जाती है।
उदाहरण: When to Use Getters/Setters vs Plain Properties
// Declare the constant `point`, set to `{ x: 5 }`
// Declare the constant `point`, set to `{ x: 5 }`
const point = { x: 5 };
// Declare the constant `circle`, set to `{ radius: 5, get area() { return Math.PI * this.radius ** 2; } }`
// Declare the constant `circle`, set to `{ radius: 5, get area() { return Math.PI * this.radius ** 2; } }`
const circle = { radius: 5, get area() { return Math.PI * this.radius ** 2; } };
// Print `point.x, circle.area` to the console
// Print `point.x, circle.area` to the console
console.log(point.x, circle.area);
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: