JavaScript တွင် Parameter မှတစ်ဆင့် Context
ပြဿနာကိုဖြေရှင်းရန် အခြားနည်းလမ်းတစ်ခုရှိပါသေးတယ်။
child function သည် parameter တစ်ခုကိုလက်ခံအောင် လုပ်ဆောင်ပါမည်။
function child(param) {
// ဤနေရာတွင်ကုဒ်ရှိလိမ့်မည်
}
ထို့အပြင် ဤ function ကိုခေါ်သောအခါ ၎င်းအတွင်းသို့
this ကိုပေးပို့ပါမည်။
function parent() {
child(this); // this ကို parameter အဖြစ်ပေးပို့သည်
function child(param) {
// param variable ထဲတွင် ပေးပို့ထားသော this ၏အကြောင်းအရာများရှိသည်
}
}
child ကိုခေါ်ဆိုခြင်းသည်
မိဘ function အတွင်းတွင်ဖြစ်သောကြောင့် ပေးပို့သော
this သည် လိုအပ်သောအရာကိုညွှန်ပြသည်။ ထို့နောက်
ဤthis သည် param parameter ထဲသို့ဝင်ရောက်ပြီး
ဤပုံစံဖြင့် function အတွင်းတွင်အသုံးပြုနိုင်မည်ဖြစ်သည်။
နောက်ဆုံးကုဒ်မှာ ဤသို့ဖြစ်သည်။
let elem = document.querySelector('#elem');
elem.addEventListener('blur', parent);
function parent() {
child(this); // this ကို parameter အဖြစ်ပေးပို့သည်
function child(param) {
console.log(param.value); // input ၏ value ကိုထုတ်ပြသည်
}
}
ယခင် တာဝန် မှ ကုဒ်ကိုယူပြီး လေ့လာထားသော ဒုတိယနည်းလမ်းကို အသုံးပြု၍ ကုဒ်၏ပြဿနာကိုပြုပြင်ပါ။