Static (स्थिर)

The static keyword defines the static methods or properties for a class. These methods and properties are called in the class itself.

'स्थिर' कीवर्डले वर्गको लागि स्थिर विधिहरू वा गुणहरू परिभाषित गर्दछ। यी विधिहरू र गुणहरू वर्गमा नै भनिन्छ।

class Car {
  constructor(name) {
    this.name = name;
  }
  static hello(x) {
    return "Hello " + x.name;
  }
}
let myCar = new Car("Toyota");

console.log(myCar.hello()); // This will throw an error
console.log(Car.hello(myCar));
// Result: Hello Toyota

One can access the static method or property of another static method of the same class using this keyword.

this कीवर्ड प्रयोग गरेर उही वर्गको अर्को स्थिर विधिको स्थिर विधि वा गुण पहुँच गर्न सक्छ।

results matching ""

    No results matching ""