Function Parameters
functions တွေက parameters တွေ သယ်ဆောင်နိုင်ပါတယ်။ functions parameters ဆိုတာက functions နဲ့အတူသတ်မှတ်ထားတဲ့ names listed လေးတွေကိုပြောတာပါ။
Example
function MyFun(parameter1,parameter2,parameter3){
//some code
}
parameter ဆိုတာတကယ်တော့ variables တွေလိုပါပဲ name လေးတွေပေးပြီး parentheses () ထဲမှာ ‘,’ ခြားပြီး ထည့်ရပါတယ်။
အဲ့ဒီလို parameters တွေကို name တွေနဲ့ သတ်မှတ်ပြီးရင် သူတို့ကို function အထဲမှာပြန်ထည့်သုံးနိုင်ပါတယ်။
Example
function sayHello(name) {
alert(“Hi, “ + name);
}
sayHello(“Mg Mg”);
//Alerts “Hi, Mg Mg”
ဒီမှာဆိုရင် function က name လို့ခေါ် တဲ့ parameter လေးတစ်ခုထည့်ထားပါတယ်။
function ကိုပြန်ခေါ် တဲ့အခါမှာ parameter ရဲ့value လို့ခေါ် တဲ့ argument ကို parentheses () ထဲမှာ ထည့်ပေးရပါတယ်။
အဲ့ ဒါကြောင့် parameters တွေကို function နဲ့အတူ value တွေပါ ပါချင်တဲ့အခါသုံးလေ့ရှိပါတယ်။
function ရဲ့အားသာချက်အတိုင်း function ကို တစ်ခါပဲ define လုပ်ပြီး parameter value ဖြစ်တဲ့ argument တွေ အမျိုးမျိုးထည့်ပြီး function execute လုပ်လို့ရပါတယ်။
Example
function sayHello(name){
alert(“Hi, “+name);
}
sayHello(“David”);
sayHello(“Sarah”);
sayHello(“John”);
Multiple Parameters
အပေါ် က ဥပမာက parameter တစ်ခုပဲယူထားတာပါ။ parameter နှစ်ခုယူထား ချင်တယ်ဆို ‘,’ လေးခံ ပြီး ထားပါတယ်။
Example
function myFunc(x ,y){
//some code
}
parameters နှစ်ခုယူရင်လည်း parentheses () ထဲမှာ ပဲ ရေးပြီး သူတို့ကို function အတွင်းထဲမှာ ပြန်အသုံးပြုနိုင်ပါတယ်။
Example
function sayHello(name,age){
document.write(name+ “ is “ + age + “years old.”);
}
sayHello(“John” , 20);
//Outputs “John is 20 years old.”
Parameters တွေကို နှစ်ခုထက်ပိုထားပြီး argument တွေထည့်ရမှာ အရမ်းများတဲ့အခါ မှာ arguments[0] , arguments[1] ဆိုပြီးတော့ သုံးလို့ရပါတယ်။ ဘာကြောင့်လဲဆို function ကို arguments(parameter’s value) နဲ့အတူ execute လုပ်ချင်တဲ့ အခါ arguments တွေကို အရင် arguments ဆိုတဲ့ array ထဲမှာ သွား သိမ်းထားပါတယ်။
Example
function sayHello(name,age){
console.log(arguments[0] +” is “ +arguments[1]+” years old.”);
}
sayHello(“Smith”, 19);
//Outputs “Smith is 19 years old. ”
သတိပြုရမည့်အချက်က JavaScript Functions တွေက arguments အရေအတွက်ကို check မလုပ်ပါဘူး ။
ဒါကြောင့် function ပြန်ခေါ် လို့ argument မပါလာတဲ့အခါ အဲ့ဒီမပါတဲ့ argument အတွက် undefined ဆိုပြီး တော့ value assign မလုပ်ထားကြောင်း ပဲပြပါလိမ့်မယ်။ function က ပုံမှန်းတိုင်း execute ဖြစ်ပါလိမ့်မယ်။
Example
function sayHello(name,age){
console.log(name + “is “ +age+” years old.”);
}
sayHello(“Smith”);
//Outputs “Smith is undefined years old.”
undefined မဖြစ်အောင် argument value တွေကို default parameter value ထားလို့ရပါတယ်။
Example
function sayHello(name=”Hla Hla”,age=21){
console.log(name +”is “+ age+” years old.”);
sayHello();
//Outputs “Hla Hla is 21 years old.”
arguments တွေမထည့်ပေမဲ့ parameter ထားကတည်းက parameter ရဲ့ value တွေကို default ထည့်လိုက်ခြင်းကြောင့် function ကိုပြန်ခေါ် တဲ့အခါ arguments တွေမထည့်ပေမဲ့ undefined ဆိုပြီးမထွက်ပါဘူး။
သတိပြုရမည့်အချက်က parameter values တွေကို default value ထားတဲ့ အခါ arguments[0], arguments[1] ဆိုပြီးသုံးရင် undefined ဖြစ်ပါလိမ့်မယ်။ ဘာကြောင့်လဲဆိုတော့ sayHello() function ကို default parameter value ထားပြီး function ကို arguments(parameter’s value) မထည့်ပဲ ပြန်ခေါ် လို့ ဖြစ်ပါတယ်။
No comments:
Post a Comment