← Back to TypeScript Course | Chapter 6: Classes | Lesson 9 of 9

Getters और Setters

Getters और setters class properties तक controlled access प्रदान करते हैं। एक getter एक property की तरह एक value पढ़ता है, जबकि एक setter आपको store करने से पहले एक value validate या transform करने देता है।
Syntax
typescript
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

typescript
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

typescript
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

typescript
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

typescript
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

typescript
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);
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक getter और setter को backing field जैसा ही नाम देना (name और name), जो हमेशा के लिए recurse करता है; _name जैसा नाम उपयोग करें।
  2. एक getter को एक method की तरह call करना, c.area(), जब इसे एक property की तरह c.area access किया जाता है।
  3. एक setter जोड़ना जो एक value return करता है या getter छोड़ देता है, इसलिए property write-only है या returned value ignore हो जाती है।
चैप्टर सारांश
  • Classes properties, एक constructor, और methods define करती हैं, public, private, और protected access modifiers के साथ।
  • readonly और static members, getters और setters, और inheritance आपको behavior control और share करने देते हैं।
  • Abstract classes subclasses के लिए base behavior प्रदान करती हैं, और classes एक contract follow करने के लिए interfaces implement कर सकती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.