JavaScript හි පරිශීලක අංගයන් ක්ලෝසර් හරහා සම්ප්රේෂණය කිරීම

අපට පහත මොඩියුලය ඇති බව සිතන්න:

javascript;(function(root, type, amount) {
  let parent = document.querySelector(root);
  
  for (let i = 1; i <= amount; i++) {
    let elem = document.createElement(type);
    parent.append(elem);
  }
})('#parent', 'p', 5);

ඔබට පෙනෙන පරිදි, මෙම මොඩියුලයට සැකසුම් තුනක් සම්ප්රේෂණය වේ: මාපිය මූලද්‍රව්‍යයේ තේරීමක්, නිර්මාණය කිරීම සඳහා මූලද්‍රව්‍යයේ වර්ගය සහ මූලද්‍රව්‍ය ගණන.

සාමාන්‍යයෙන් එවැනි සැකසුම් වස්තුවක ආකාරයෙන් සකසා ඇත:

javascriptlet config = {
  root:  '#parent',
  type:  'p',
  amount: 5
}

අපගේ වස්තුව පරාමිතියක් ලෙස මොඩියුලයට සම්ප්රේෂණය කරමු:

javascript;(function(config) {
  let parent = document.querySelector(config.root);
  
  for (let i = 1; i <= config.amount; i++) {
    let elem = document.createElement(config.type);
    parent.append(elem);
  }
})(config);

සැකසුම් සහිත වස්තුව විකේන්ද්‍රීකරණය කිරීම වඩාත් පිළිගත හැකිය:

javascript;(function({root, type, amount}) {
  let parent = document.querySelector(root);
  
  for (let i = 1; i <= amount; i++) {
    let elem = document.createElement(type);
    parent.append(elem);
  }
})(config);