JavaScript တွင် function parameter များ destructuring လုပ်ခြင်း

Destructuring သည် အလွန်အရေးကြီးသော အသုံးချနယ်ပယ်တစ်ခုကို ထပ်မံပိုင်ဆိုင်ထားသည် - ၎င်းမှာ function parameter များကို လက်ခံခြင်းဖြစ်သည်။ အဓိကအချက်မှာ - function သည် parameter တစ်ခုအနေဖြင့် array တစ်ခုကိုလက်ခံပါက၊ function ကိုကြေငြာသည့်နေရာတွင်ပင် ၎င်းအား မည်သို့ destructuring လုပ်ရမည်ကို သတ်မှတ်နိုင်ခြင်းဖြစ်သည်။

ဥပမာတစ်ခုဖြင့် ကြည့်ကြပါစို့။ ကျွန်ုပ်တို့တွင် ခုနှစ်၊ လ နှင့် ရက်ကို parameter အဖြစ်လက်ခံသော function တစ်ခုရှိသည်ဆိုပါစို့။

javascriptfunc([2025, 12, 31]);

Function ၏ parameter တွင်ပင် ဤ array ကို မည်သည့်အရာများအဖြစ် ခွဲခြားရမည်ကို သတ်မှတ်ကြပါစို့။

javascriptfunction func([year, month, day]) {
  console.log(year);  // 2025 ကိုပြသမည်
  console.log(month); // 12 ကိုပြသမည်
  console.log(day);   // 31 ကိုပြသမည်
}

အထက်ပါတည်ဆောက်ပုံကို function ၏ parameter တစ်ခုအနေဖြင့် သတ်မှတ်ရမည်။ လိုအပ်ပါက parameter အပိုများထည့်နိုင်သည်။

javascriptfunc('str1', [2025, 12, 31], 'str2');

function func(param1, [year, month, day], param2) {
  console.log(param1); // 'str1' ကိုပြသမည်
  
  console.log(year);   // 2025 ကိုပြသမည်
  console.log(month);  // 12 ကိုပြသမည်
  console.log(day);    // 31 ကိုပြသမည်
  
  console.log(param2); // 'str2' ကိုပြသမည်
}

အောက်ပါဥပမာတွင် function ၏ ပထမနှင့် ဒုတိယ parameter များအဖြစ် array နှစ်ခုကိုပို့ပြီး ၎င်းတို့နှစ်ခုလုံးကို destructuring လုပ်ထားသည်။

javascriptfunc([2025, 12, 31], [2026, 11, 30]);

function func([year1, month1, day1], [year2, month2, day2]) {
  console.log(year1);  // 2025 ကိုပြသမည်
  console.log(month1); // 12 ကိုပြသမည်
  console.log(day1);   // 31 ကိုပြသမည်
  
  console.log(year2);  // 2026 ကိုပြသမည်
  console.log(month2); // 11 ကိုပြသမည်
  console.log(day2);   // 30 ကိုပြသမည်
}
Задание 1

အောက်ပါကုဒ်ကို သင်ယူထားသောသဘောတရားအရ destructuring ဖြင့် ပြန်လည်ပြင်ဆင်ပါ။

javascriptfunction func(employee) {
  let name       = employee[0];
  let surname    = employee[1];
  let department = employee[2];
  let position   = employee[3];
  let salary     = employee[4];
}

func( ['John', 'Smit', 'development', 'programmer', 2000] );
Задание 2

အောက်ပါကုဒ်ကို သင်ယူထားသောသဘောတရားအရ destructuring ဖြင့် ပြန်လည်ပြင်ဆင်ပါ။

javascriptfunction func(employee) {
  let name     = employee[0];
  let surname  = employee[1];
  let info     = employee[2];
}

func( ['John', 'Smit', 'development', 'programmer', 2000] );
Задание 3

အောက်ပါကုဒ်ကို သင်ယူထားသောသဘောတရားအရ destructuring ဖြင့် ပြန်လည်ပြင်ဆင်ပါ။

javascriptfunction func(employee) {
  let name       = employee[0];
  let surname    = employee[1];
  let department = employee[2];
  
  let position;
  if (arr[3] !== undefined) {
    position = arr[3];
  } else {
    position = 'junior';
  }
}

func( ['John', 'Smit', 'development'] );
Задание 4

အောက်ပါကုဒ်ကို သင်ယူထားသောသဘောတရားအရ destructuring ဖြင့် ပြန်လည်ပြင်ဆင်ပါ။

javascriptfunction func(department, employee, hired) {
  let name     = employee[0];
  let surname  = employee[1];
  
  let year  = hired[0];
  let month = hired[1];
  let day   = hired[2];
}

func( 'development', ['John', 'Smit'], [2018, 12, 31] );