Getters और Setters
In this page:
class ClassName {
private _prop: type;
get prop(): type {
return this._prop;
}
set prop(value: type) {
this._prop = value;
}
}
Basic Getter
एक getter get keyword से declare होता है और इसे एक method की तरह call करने के बजाय एक plain property की तरह access किया जाता है, जो ordinary property syntax के पीछे एक computed या validated value expose करने के लिए उपयोगी है।
उदाहरण: Basic Getter
class Circle {
constructor(private radius: number) {}
get area(): number {
return 3.14 * this.radius ** 2;
}
}
const c = new Circle(5);
console.log(c.area); // accessed like a property
Basic Setter
एक setter set keyword से declare होता है और assignment syntax उपयोग करके invoke होता है; setters एक class को किसी property पर हर write को intercept करने देते हैं बजाय direct, unchecked field access allow करने के।
उदाहरण: Basic Setter
class Person {
private _name: string = "";
set name(value: string) {
this._name = value;
}
get name(): string {
return this._name;
}
}
const p = new Person();
p.name = "Kavya";
console.log(p.name);
Setters के साथ Validation
Setters incoming values को assign करने से पहले validate कर सकते हैं, एक invalid update को throw या ignore करते हुए, जो एक object की internal state को हमेशा एक consistent, trustworthy shape में रखने में मदद करता है।
उदाहरण: Validation with Setters
class Account {
private _balance: number = 0;
set balance(value: number) {
if (value < 0) throw new Error("Balance cannot be negative");
this._balance = value;
}
get balance(): number {
return this._balance;
}
}
const acc = new Account();
acc.balance = 100;
console.log(acc.balance);
Computed Getters
एक getter एक या ज़्यादा internal properties से fly पर एक value calculate कर सकता है, जो derived values को अपने आप sync में रखता है बजाय class को एक अलग cached field update करना याद रखने की ज़रूरत पड़ने के।
उदाहरण: Computed Getters
class Rectangle {
constructor(public width: number, public height: number) {}
get area(): number {
return this.width * this.height;
}
}
console.log(new Rectangle(4, 5).area);
Getters और Setters को साथ उपयोग करना
Getters और setters को अक्सर callers को एक property-like interface देने के लिए pair किया जाता है जबकि underlying storage को private रखते हुए, implementation details को ऐसी syntax के पीछे छिपाते हुए जो अब भी एक simple field access जैसी दिखती है।
उदाहरण: Using Getters and Setters Together
class Temperature {
private _celsius: number = 0;
get fahrenheit(): number {
return this._celsius * 9 / 5 + 32;
}
set fahrenheit(value: number) {
this._celsius = (value - 32) * 5 / 9;
}
}
const t = new Temperature();
t.fahrenheit = 98.6;
console.log(t.fahrenheit);
- एक getter और setter को backing field जैसा ही नाम देना (
nameऔरname), जो हमेशा के लिए recurse करता है;_nameजैसा नाम उपयोग करें। - एक getter को एक method की तरह call करना,
c.area(), जब इसे एक property की तरहc.areaaccess किया जाता है। - एक setter जोड़ना जो एक value return करता है या getter छोड़ देता है, इसलिए property write-only है या returned value ignore हो जाती है।
- Classes properties, एक constructor, और methods define करती हैं,
public,private, औरprotectedaccess modifiers के साथ। readonlyऔरstaticmembers, getters और setters, और inheritance आपको behavior control और share करने देते हैं।- Abstract classes subclasses के लिए base behavior प्रदान करती हैं, और classes एक contract follow करने के लिए interfaces implement कर सकती हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: