Closures - Understanding closures and lexical scope

 

Closures - Fahamka "Closures" iyo "Lexical Scope"

JavaScript gudaheeda, mid ka mid ah fikradaha ugu awoodda badan isla markaana mararka qaar si khaldan loo fahmo waa closures. Closures waxay u ogolaadaan in functions-ku ay helaan (access) variables ku jira scope-ka function-ka weyn, xitaa ka dib marka function-kaas uu dhammeeyo fulintiisa. Astaamahan ayaa kor u qaada awoodda barnaamijyada functional programming ee JavaScript waxayna door muhiim ah ka ciyaaraan ilaalinta xogta gaarka ah iyo abuurista functions ka sarreeya (higher-order functions).

closures  JavaScript
closures  JavaScript 


Waa Maxay "Closures"?

Closure waxaa la sameeyaa marka function lagu qeexo function kale gudaheeda, taasoo u oggolaanaysa function-ka gudaha inuu xasuusto oo uu galo (access) variables-ka function-ka dibadda xitaa kadib marka uu function-ka dibadda dhammeeyo. Si kale haddii loo dhigo, function-ka gudaha ayaa "xidhaya" (closes over) variables-ka laga dhaxlay scope-ka luqadda (lexical scope). Tani waxay ka dhigan tahay in xiriirinta (bindings) variables-ka la haysto xasuusta, taasoo faa’iido badan leh balse haddii si aan habooneyn loo maamulo ay keeni karto dhibaatooyin xagga xusuusta (memory leaks).

Lexical Scope

Lexical Scope waxay tilmaamaysaa in helitaanka variables-ka lagu go’aamiyo qaab-dhismeedka qoraalka (code) ee la qorey. Variables-ka lagu qeexay meel ka baxsan function waxaa laga heli karaa gudaha function-ka haddii ay ku jiraan silsiladda scope-ka. Qodobka ugu muhiimsan waa in helitaankaasi lagu go’aamiyo waqtiga la qoro koodhka, halkii laga go’aamin lahaa waqtiga uu function-ku fulinayo. Sidaas darteed, scope-ka function-ka ayaa la dejiyay markii la qoray, waxaana uu la socdaa function-ka meel kasta uu tago.

Tusaale Fudud oo Ah "Closure"

Bal aynu eegno tusaalahan hoose ee JavaScript:

javascript
function sameeTiroKorodhsiye() { let count = 0; // 'count' waa variable gudaha sameeTiroKorodhsiye // Function-ka gudaha waa closure oo qabtay variable-ka 'count' return function() { count += 1; return count; }; } const counter = sameeTiroKorodhsiye(); console.log(counter()); // Wax-soo-saarka: 1 console.log(counter()); // Wax-soo-saarka: 2 console.log(counter()); // Wax-soo-saarka: 3

Tusaalahan, function-ka sameeTiroKorodhsiye wuxuu qeexayaa variable-ka count kadibna wuxuu soo celiyaa function gudaha ah oo kordhiya qiimaha count kuna soo celiya. Xitaa marka function-ka sameeTiroKorodhsiye uu dhammaado, function-ka la soo celiyey wuxuu weli heli karaa variable-ka count sababo la xiriira closure-ka. Mar kasta oo la waco function-ka counter, wuxuu xusuustaa qiimaha hore ee count, taasoo muujinaysa sida closures ay ugu ogolaadaan in xogta la keydiyo inta uu socdo fulinta functions kala duwan.

Faa’iidooyinka Isticmaalka "Closures"

  1. Xog Qarinta iyo Isku-Dhafidda Xogta: Closures waxay bixiyaan hab lagu abuuro variables gaar ah oo aan laga heli karin meelaha kale ee global scope. Tani waa qaab caan ah marka la dhisayo modules-ka, halkaas oo xaaladda gudaha la doonayo in laga qariyo isticmaalaha.
  2. Barnaamijyada Functional-ka: Closures waxay suurto gelinayaan in la isticmaalo qaabab functional sida currying iyo partial application. Waxay ka caawiyaan in functions-ka la isku soo ururiyo (compose) si wax ku ool ah.
  3. Maaraynta Dhacdooyinka (Event Handling): Barnaamijyada ku saleysan dhacdooyinka, closures waxay ka caawiyaan in la xasuusto variables-ka muhiimka ah ee la xiriira dhacdooyinka, xitaa marka dhacdadani ay dhacdo kadib markii function-ka asalka ah uu fulay
  4.  
    closures  JavaScript
    closures  JavaScript

Qodobo Muhiim Ah oo Laga Digtoonaado

In kasta oo closures ay yihiin kuwo awood badan, waxaa muhiim ah in si taxadar leh loo isticmaalo. Haynta variables-ka muddo dheer waxay keeni kartaa in xusuusta la isticmaalo badan, taasoo saameyn karta waxqabadka codsiyada soconaya muddo dheer. Fahamka wareegga nolosha (lifecycle) ee closures waa muhiim si looga fogaado dhibaatooyinka xagga xusuusta.

Gabagabo

Closures waa astaamo asaasi ah oo JavaScript ah kuwaas oo u ogolaada functions-ku inay xasuustaan scope-kooda luqadeed, xitaa marka laga baxo macnaha ay ku jiraan. Fikraddan ayaa saldhig u ah farsamooyinka casriga ah ee barnaamijyada JavaScript, laga bilaabo abuurista variables gaar ah ilaa qorista code nadiif ah oo functional ah. Marka la barto oo la fahmo closures, horumariyeyaashu waxay awood u yeeshaan inay qoraan code modular ah, ammaan ah, oo si fudud loo dayactiri karo, iyagoo ka faa’iideysanaya dabacsanaanta uu bixiyo paradigm-ka functions ee JavaScript.

Previous Post Next Post

Important

Important