What is static class in Typescript | Static class in Hindi | TS Tutorial-20

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024
  • In TypeScript, the static keyword is used to define class members that belong to the class itself rather than to any instance of the class. Static members can include properties and methods that are shared by all instances of the class and can be accessed directly on the class itself without creating an instance.
    Static Properties
    Static properties are variables that are shared across all instances of a class. They can be accessed directly on the class.
    class MyClass {
    static staticProperty: string = 'I am a static property';
    static logStaticProperty() {
    console.log(MyClass.staticProperty);
    }
    }
    // Accessing static property
    console.log(MyClass.staticProperty); // Output: I am a static property
    Static Methods
    Static methods are functions that belong to the class itself rather than any object created from the class. These methods can be called on the class itself.
    class Calculation {
    static square(x: number): number {
    return x * x;
    }
    static cube(x: number): number {
    return x * x * x;
    }
    }
    // Calling static methods
    console.log(Calculation.square(5)); // Output: 25
    console.log(Calculation.cube(3)); // Output: 27

Комментарии •