நிலையானவை (Static)

static முக்கிய சொல் ஒரு வகுப்பிற்கான நிலையான முறைகள் அல்லது பண்புகளை வரையறுக்கிறது. இந்த முறைகள் மற்றும் பண்புகள் வகுப்பிலேயே அழைக்கப்படுகின்றன.

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

console.log(myCar.hello()); // இது பிழையை (error) எறியும்
console.log(Car.hello(myCar));
// முடிவு: Hello Toyota

this முக்கிய சொல்லைப் பயன்படுத்தி அதே வகுப்பின் மற்றொரு நிலையான முறையின் (static method) நிலையான முறை அல்லது பண்பை ஒருவர் அணுகலாம்.

results matching ""

    No results matching ""