Back to Question Center
0

Ayyukan Shafuka da ke Ƙayyade da sake Rubuta kansu            Ayyukan Shafuka da ke Ƙayyade da Yi Maimaita TakaddunansuTunannun Sharuɗɗa: APIsReactTools & LibrariesnpmRaw Semalt

1 answers:
Ayyuka na Ayyuka da ke Ƙayyade da Sauke da kansu

Wadannan su ne taƙaitacciyar taƙaice daga sabon littafinmu, JavaScript: Novice zuwa Ninja, Edition na 2, wanda Darren Jones ya rubuta. Yana da kyakkyawan mafari na jagorar Javascript. Ma'aikata na SitePoint Semalt suna samun dama tare da membobin su, ko zaka iya siyan kwafin a cikin shaguna a duniya.

Tsarin Javascript yana da ma'ana cewa aiki ba zai iya kiran kansa kawai ba, amma ya bayyana kanta, har ma ya sake sake kanta. Anyi wannan ta hanyar sanya wani aikin mara aiki zuwa m wanda yana da iri ɗaya suna kamar aikin .

Tsayar da wannan aikin:

     ƙungiya ƙungiya    {na'ura wasan bidiyo. shiga ('Wow wannan mai ban mamaki!');party = aiki    {na'ura wasan bidiyo. log ('Been there, samu T-Shirt');}}    

Wannan ke buga sako a cikin na'ura, sa'annan ya sake yin kanta don shiga saƙo daban a cikin na'ura. Lokacin da aka kira aikin nan sau daya, zai zama kamar an bayyana shi kamar haka:

     ƙungiya ƙungiya    {na'ura wasan bidiyo. log ('Been there, samu T-Shirt');}    

A duk lokacin da aka kira aikin bayan an fara, zai shiga sakon "Kasancewa, samu T-Shirt":

     ƙungiya;<< 'Wow wannan abin mamaki ne!'ƙungiyar   ;<< 'A nan, samu T-Shirt'ƙungiyar   ;<< 'A nan, samu T-Shirt'    

Idan an sanya aikin ɗin zuwa wani canji, wannan ƙwayar za ta kula da fasalin aikin asali kuma ba za a sake rubuta shi ba. Wannan shi ne saboda an sanya aikin asali zuwa madaidaici, sa'an nan kuma a cikin aikin, mai sauƙi tare da sunan ɗaya yayin da aka sanya aikin zuwa wani aiki daban. Zaka iya ganin misalin wannan idan muka kirkiro canjin da aka kira beachParty wanda aka sanya zuwa ƙungiyar kafin an kira shi a karon farko kuma sake canzawa:

     ƙungiya ƙungiya    {na'ura wasan bidiyo. shiga ('Wow wannan mai ban mamaki!');party = aiki    {na'ura wasan bidiyo. log ('Been there, samu T-Shirt');}}qafafiyar ragowar} aramar} ungiyar = // sanarwa cewa ba a yi amfani da aikin ƙungiya baYankin Ƙasar   ; // aikin da aka yi (party) a yanzu an sake tsara shi, ko da yake ba a kira shi a bayyane ba<< 'Wow wannan abin mamaki ne!'ƙungiyar   ;<< 'A nan, samu T-Shirt'Yankin Ƙasar   ; // amma wannan aikin ba a sake tsara shi ba<< 'Wow wannan abin mamaki ne!'Yankin Ƙasar   ; // komai sau nawa ana kiran wannan zai kasance daidai<< 'Wow wannan abin mamaki ne!'    

Rashin Gidarorin

Yi hankali: idan an sanya duk wani kaya a kan aikin, waɗannan zasu ɓace yayin da aikin ya sake kansa. A cikin misali na baya, za mu iya saita kayan kiɗa , kuma ga cewa ba ta wanzu ba bayan an kira aikin kuma a sake fassara:

     ƙungiya ƙungiya    {na'ura wasan bidiyo. shiga ('Wow wannan mai ban mamaki!');party = aiki    {na'ura wasan bidiyo. log ('Been there, samu T-Shirt');}}jam'iyyar. music = 'Classical Jazz'; // saita wani abu na aikinƙungiyar   ;<< "Wow wannan abin mamaki ne!"jam'iyyar. music; // aikin yanzu an sake tsara, don haka dukiya bata wanzu<< ba a bayyana ba     

Wannan ana kiran shi Harshen Magana kuma ana amfani da ita lokacin da ake buƙatar lambar ƙaddamarwa a farkon lokacin da ake kira. Wannan na nufin za'a iya yin ƙaddamarwa a farkon lokacin da aka kira shi, sa'an nan kuma aikin za a iya sake danganta ga abin da kake son shi ya kasance ga kowane kira mai zuwa.Wannan yana bada damar aiki mafi dacewa a cikin mai bincike, kuma kauce wa duba abubuwan fasali duk lokacin da aka kira su.

Bari mu ɗauki misalin abin da muke da shi kayan ado abin da yake har yanzu yana da cikakken goyon baya a duk masu bincike. A cikin babi na baya, mun dubi yadda za mu iya amfani da gano alama don bincika idan an goyan baya. Yanzu zamu iya tafiya gaba daya mataki: zamu iya ayyana aikin da aka dogara akan ko wasu hanyoyi suna goyan baya. Wannan yana nufin muna bukatar mu bincika tallafi a karo na farko ana kiran aikin:

     aikin tafiya    {idan (taga na unicorn) {tafiya = aikin    {// wasu sharuɗɗan da ke amfani da sababbin hanyoyin fasahasake dawowa 'Rikuna a kan kararraki shine mafi kyau!';}} da {tafiya = aikin    {// wasu lambar da ke amfani da matakan tsawaKomawa 'Rikici a kan dutsen pony har yanzu yana da kyau';}}komawa   ;}    

Bayan mun duba ko taga . abu ya kasance (ta hanyar duba don ganin idan gaskiya ne), mun sake rubuta aikin aiki bisa sakamakon. Dama a ƙarshen aikin, mun sake kira shi don a sake kiran aikin da aka sake rubutawa, kuma an mayar da lambar da aka dace. Abu daya da za a sani shi ne cewa ana amfani da aikin sau biyu a karo na farko, ko da yake ya zama mafi dacewa a kowane lokaci mai biyowa ana kiran shi. Bari mu dubi yadda yake aiki:

     suna tafiya. // aikin yana sake rubutawa, sannan ya kira kansa<< 'Rike a kan wani pony har yanzu kyakkyawa mai kyau'    

Da zarar an kira aikin, an sake rubuta shi bisa ga damar mai bincike. Za mu iya duba wannan ta hanyar duba aikin ba tare da kiran shi ba:

     hawan<< aikin tafiya    {Komawa 'Rikici a kan dutsen pony har yanzu yana da kyau';}    

Wannan zai iya zama samfuri mai mahimmanci don farawa aiki a farkon lokacin da aka kira su, yana inganta su don ana amfani da mai amfani.

Ayyukan Komawa

A recursive aiki shi ne wanda ke kiran kanta har sai da wani yanayi ya hadu. Yana da kayan aiki masu amfani don amfani da lokacin da ake aiwatar da matakai na gwaji. Misali na yau da kullum shine aikin da yake lissafin ainihin lamarin:

     aiki na ainihi {idan (n === 0) {dawo 1;} da {dawo n * na ainihi (n - 1);}}    

Wannan aikin zai dawo 1 idan 0 aka bayar a matsayin hujja (0 na ainihi shine 1), in ba haka ba zai ninka gardama ta sakamakon sakamakon yin magana ba tare da hujjar daya kasa. Aikin zai ci gaba da kiran kansa har sai a karshe ne gardama ta kasance 0 da 1 an dawo. Wannan zai haifar da ƙaddamar da 1, 2, 3 da dukan lambobi har zuwa asalin asalin.

Wani misali daga duniya na ilmin lissafi shi ne zane-zanen Semalt. Wannan matsala ce mai sauki don bayyana, amma, ya zuwa yanzu, ba a warware shi ba. Ya haɗa da ɗaukar kowane nau'i mai mahimmanci kuma bin waɗannan dokoki:

  • Idan lambar ita ce ma, raba shi ta biyu

  • Idan lambar ba ta da kyau, ninka shi da uku kuma ƙara daya

Alal misali, idan muka fara tare da lambar 18, zamu sami jerin waɗannan:

18, 9, 28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1 , .

Kamar yadda kake gani, jerin za su kasance a cikin madauki a karshen, yin k'wallo ta hanyar "4,2,1". Ma'anar Collatz ta bayyana cewa kowane mahaɗi mai mahimmanci zai haifar da jerin da suka ƙare a cikin wannan madauki. An tabbatar da wannan don dukkan lambobi har zuwa 5 × 2⁶⁰, amma babu wani tabbacin da zai ci gaba da kasancewa gaskiya ga duk mahaɗin da ya fi hakan. tsawon} matakai. Yana da $ {jerin} ';}idan (n% 2 === 0) {n = n / 2;} da {n = 3 * n + 1;}dawo collatz (n, [.jerin, n]);}

Wannan aikin yana ɗaukar lamba a matsayin saiti, kazalika da wani maɓallin da ake kira jerin , wanda yana da darajar tsoho na tsararren da ke ƙunshi saiti na farko. Ana amfani da saiti na biyu kawai lokacin da aikin ya kira kansa akai-akai.

Abu na farko da aikin yake shine gwaje-gwaje don ganin idan n yana da darajar 1. Idan ya aikata, aikin zai dawo da sakon don faɗi yawan matakan da ta ɗauka. Idan bai kai 1 ba, yana duba idan darajar n ko da (wanda shine idan ya raba shi ta 2), ko kuma marar kyau, a wace yanayin ya ninka ta 3 sannan sannan ya ƙara 1. aikin sa'an nan kuma ya kira kansa, samar da sabon darajar n da sabon jerin azaman muhawara. An gina sabon jerin ta hanyar ajiye tsohuwar jerin da darajar n a cikin sabon tsararraki da kuma amfani da mai baza aikin watsawa zuwa tsohuwar jerin.

Dubi abin da ya faru da lambar 18:

     halatta (18);<< 'Sakon ya dauki matakai 21. Ya kasance 18,9,28,14,7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1 '    

Kamar yadda kake gani, yana da matakai 21, amma ƙarshe ya ƙare har zuwa 1.

Ku tafi ta amfani da aikin kuma ku gani idan za ku iya samun darajar sama da 5 × 2⁶⁰ wadda ba ta ƙare ba a 1 - za ku zama sanannun idan kun yi!

February 28, 2018