JavaScript Static Methods
Static class method တွေကို class ကိုယ်တိုင်က သတ်မှတ်ပါတယ်။
static
အရာဝတ္ထုတစ်ခုပေါ်ရှိ နည်းလမ်းတစ်ခု၊ အရာဝတ္ထုအတန်းအစားတွင် သာ သင်ခေါ်ဆို၍ မရပါ။
ဥပမာ
class Car {
constructor(name) {
this.name =
name;
}
static hello() {
return "Hello!!";
}
}
let myCar = new Car("Ford");
// You can call 'hello()' on
the Car Class:
document.getElementById("demo").innerHTML
= Car.hello();
// But NOT on a Car Object:
// document.getElementById("demo").innerHTML
= myCar.hello();
// this will raise an error.
နည်းလမ်း အတွင်းရှိ myCar အရာဝတ္ထုကို အသုံးပြုလိုပါက static
၊ ၎င်းကို ကန့်သတ်ချက်များအဖြစ် ပေးပို့နိုင်ပါသည်။
ဥပမာ
class Car {
constructor(name) {
this.name =
name;
}
static hello(x) {
return "Hello " +
x.name;
}
}
let myCar = new Car("Ford");
document.getElementById("demo").innerHTML
= Car.hello(myCar);