Closures in Javascript

In simple terms,a function in javascript always remembers the variables and properties outside it also called as its lexical environment. Simply means that function in javascript is binded to outer environment called as closures.




𝗦𝗼𝗺𝗲 𝗣𝗼𝗶𝗻𝘁𝘀 𝗧𝗼 𝗨𝗻𝗱𝗲𝗿𝘀𝘁𝗮𝗻𝗱 :-

1. 𝗛𝗲𝗿𝗲 𝗶𝗻 𝗲𝘅𝗮𝗺𝗽𝗹𝗲 𝘄𝗲 𝗵𝗮𝘃𝗲 𝘁𝗵𝗿𝗲𝗲 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀. 𝗢𝘂𝘁𝗲𝗿 , 𝗶𝗻𝗻𝗲𝗿 𝗮𝗻𝗱 𝗺𝗼𝘀𝘁𝗜𝗻𝗻𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻.


2. 𝗪𝗵𝗲𝗻 𝘄𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗲 𝗢𝘂𝘁𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗶𝘁 𝗿𝗲𝘁𝘂𝗿𝗻𝘀 𝘁𝗵𝗲 𝗶𝗻𝗻𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗮𝗻𝗱 𝘄𝗲 𝘀𝘁𝗼𝗿𝗲 𝗶𝘁 𝗶𝗻 𝗶𝗻𝗻𝗲𝗿 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲.

3. 𝗪𝗵𝗲𝗻 𝘄𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗲 𝘁𝗵𝗲 𝗶𝗻𝗻𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗶𝘁 𝗿𝗲𝘁𝘂𝗿𝗻𝘀 𝘁𝗵𝗲 𝗺𝗼𝘀𝘁𝗜𝗻𝗻𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻.

4. 𝗕𝘂𝘁 𝘄𝗵𝗲𝗻 𝗪𝗲 𝗲𝘅𝗲𝗰𝘂𝘁𝗲 𝗺𝗼𝘀𝘁𝗜𝗻𝗻𝗲𝗿 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗶𝘁 𝗵𝗮𝘀 𝘁𝗼 𝗰𝗼𝗻𝘀𝗼𝗹𝗲 𝗹𝗼𝗴 𝘁𝗵𝗲 𝗼𝘂𝘁𝗽𝘂𝘁.

5. 𝗡𝗼𝘄 𝗤𝘂𝗲𝘀𝘁𝗶𝗼𝗻 𝗰𝗼𝗺𝗲𝘀 𝗱𝗼𝗲𝘀 𝘁𝗵𝗶𝘀 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻 𝗵𝗮𝘃𝗲 𝘃𝗮𝗿𝗶𝗮𝗯𝗹𝗲𝘀 (𝗻𝗮𝗺𝗲 𝗮𝗻𝗱 𝗰𝗶𝘁𝘆) NO right it has only one local variable score. But this function formed binding with its outer environment (𝗰𝗹𝗼𝘀𝘂𝗿𝗲𝘀 𝗼𝘂𝘁𝗲𝗿 𝗮𝗻𝗱 𝗶𝗻𝗻𝗲𝗿 ).

6. 𝗔𝗻𝗱 𝗶𝘁 𝗿𝗲𝗺𝗲𝗺𝗯𝗲𝗿𝘀 𝗮𝗹𝗹 𝘃𝗮𝗹𝘂𝗲𝘀 𝗲𝘃𝗲𝗻 𝘁𝗵𝗼𝘂𝗴𝗵 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗼𝘂𝘁𝗲𝗿 𝗮𝗻𝗱 𝗶𝗻𝗻𝗲𝗿 𝗻𝗼 𝗹𝗼𝗻𝗴𝗲𝗿 𝗲𝘅𝗶𝘁𝘀 𝗶𝗻 𝗰𝗮𝗹𝗹 𝘀𝘁𝗮𝗰𝗸.


#Javascript #Closures #Comment #Share

Comments

Popular Posts