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

Static Members की जानकारी

Static members individual objects के बजाय class खुद के होते हैं। उन्हें class name के through access किया जाता है और वे shared values और utility methods के लिए उपयोगी हैं।
Syntax
typescript
class ClassName {
  static property: type = value;
  static method(): returnType {
    // body
  }
}
ClassName.method();

Static Properties

एक static property class खुद द्वारा shared होती है और इसे एक instance के बजाय class name उपयोग करके access किया जाता है, इसलिए इसे पढ़ने या update करने के लिए सिर्फ एक object बनाने की ज़रूरत नहीं है।

उदाहरण: Static Properties

typescript
class Counter {
  static count: number = 0;
}
Counter.count++;
console.log(Counter.count);

Static Methods

Static methods class name उपयोग करके call किए जाते हैं और उपयोगी हैं जब एक operation किसी particular instance के data पर depend न करे, जैसे एक helper जो उस class के objects बनाता या validate करता है।

उदाहरण: Static Methods

typescript
class MathUtils {
  static square(n: number): number {
    return n * n;
  }
}
console.log(MathUtils.square(5));

Static और Instance Members

Instance members individual objects के होते हैं, जबकि static members पूरी class के होते हैं, इसलिए एक static property बदलना पूरे program में इसके हर reference को एक साथ affect करता है।

उदाहरण: Static and Instance Members

typescript
class Counter {
  static total: number = 0;
  constructor() {
    Counter.total++;
  }
}
new Counter();
new Counter();
console.log(Counter.total);

Static Members और this

Static methods किसी single instance के बजाय class के context में operate करते हैं, इसलिए वे instance properties refer करने के लिए this उपयोग नहीं कर सकते — एक static method के अंदर refer करने के लिए बस कोई instance ही नहीं होता।

उदाहरण: Static Members and this

typescript
class Config {
  static appName: string = "MyApp";
  static getName(): string {
    return Config.appName; // uses class name, not "this"
  }
}
console.log(Config.getName());

Static Members के Common उपयोग

Static members आमतौर पर constants, counters, factory methods, और utility functions के लिए उपयोग होते हैं जो logically एक class के साथ belong करते हैं लेकिन अपना काम करने के लिए per-object state की ज़रूरत नहीं रखते।

उदाहरण: Common Uses of Static Members

typescript
class IdGenerator {
  private static nextId: number = 1;
  static generate(): number {
    return IdGenerator.nextId++;
  }
}
console.log(IdGenerator.generate(), IdGenerator.generate());
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. एक static member को एक instance के through access करना, जैसे new Counter().count, जब इसे Counter.count के रूप में reach किया जाना चाहिए।
  2. एक static method के अंदर this.someInstanceProperty उपयोग करना, जब कोई instance ही नहीं होता इसलिए यह instance members नहीं देख सकता।
  3. यह उम्मीद करना कि हर object के पास एक static property की अपनी copy है, जब यह value पूरी class द्वारा shared होती है।

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.