Babban matsala tare da sake dawowa shine zai iya haifar da madaukai marasa iyaka. Idan ka ƙirƙiri kewayon lambobi ta amfani da maimaitawa, sannan ka yi ƙoƙarin samun dama ga lamba a cikin wannan kewayon ta amfani da tsarin maimaitawa iri ɗaya, JavaScript a ƙarshe zai ƙare da ƙwaƙwalwar ajiya da faɗuwa.
function range(start, end) { if (start === end) { return [start]; } else { return [start].concat(range(start + 1, end)); } }
Wannan aiki ne mai maimaitawa wanda ke ɗaukar ƙimar farawa da ƙarshen kuma yana dawo da jeri na duk lambobi tsakanin waɗannan dabi'u biyu. Idan ƙimar farawa da ƙarshen suna ɗaya, kawai yana dawo da tsararru tare da wannan ƙimar. In ba haka ba, yana dawo da tsararru tare da ƙimar farawa, sannan ya sake kiran kansa tare da ƙimar farawa ta ƙaru da ɗaya kuma yana haɗa wannan sakamakon zuwa ƙarshen tsararrun.
Enum library
Laburaren Enum ɗakin karatu ne na JavaScript wanda ke ba da ingantacciyar hanya don aiki tare da ƙididdiga. Yana ba da saitin APIs waɗanda ke sauƙaƙe ƙirƙira, karantawa, sabuntawa, da ƙididdige ƙididdiga a cikin ƙidayar.
Ana iya amfani da ɗakin karatu na Enum don ƙirƙirar ƙididdiga don kowane nau'in bayanai. Misali, zaku iya amfani da shi don ƙirƙirar ƙididdiga na launuka, lambobi, kirtani, abubuwa, ko kowane nau'in bayanai.
Laburaren Enum kuma yana ba da ingantacciyar hanya don samun damar ƙimar kowane mutum a cikin ƙidayar. Kuna iya amfani da hanyoyin samun () da saita() don samun damar ƙimar kowane mutum a cikin ƙidayar. Hanyar samun () tana mayar da ƙima a ƙayyadadden matsayi a cikin ƙidayar, yayin da hanyar saiti () ke saita ƙimar a ƙayyadadden matsayi a cikin ƙidayar zuwa ƙimar da aka bayar.
Ƙididdigar ƙididdiga
Ƙididdiga hanya ce ta haɗa ɗorawa tare. Suna kama da tsararru, amma suna da ƙayyadaddun adadin abubuwa.
Ana iya amfani da ƙididdiga don adana ƙima daban-daban don wani maɓalli na musamman. Misali, zaku iya amfani da lissafi don adana launuka daban-daban waɗanda za'a iya amfani da su a cikin takaddar HTML.
Kuna iya ƙirƙirar ƙididdigewa a cikin JavaScript ta amfani da aikin Enum(). Hakanan zaka iya amfani da aikin Enum() don samun dama ga takamaiman ƙididdiga a cikin ƙidayar.
Samun shiri
Akwai 'yan hanyoyi don samun damar yin amfani da shirye-shirye zuwa bayanai a cikin JavaScript. Hanyar da ta fi dacewa ita ce amfani da DOM. Kuna iya samun dama ga DOM ta amfani da samfurin abu (DOM). Wannan abu ya ƙunshi duk bayanan game da takaddun da kuke aiki da su. Kuna iya amfani da wannan bayanin don samun damar duk abubuwan da ke cikin takaddar, da halayensu da ƙimar su.
Wata hanya don samun damar yin amfani da shirye-shirye zuwa bayanai ita ce ta JSON. JSON tsari ne da ake amfani dashi don adana bayanai. Kuna iya amfani da JSON don adana bayanai a cikin tsarin kirtani, ko kuna iya amfani da shi don adana bayanai a tsarin abu. Hakanan zaka iya amfani da JSON don adana bayanai a tsarin tsararru. Kuna iya samun damar yin amfani da shirin zuwa JSON ta amfani da json module.