Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,634 members, 7,801,828 topics. Date: Friday, 19 April 2024 at 01:02 AM

Understand Javascript Closures - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Understand Javascript Closures (388 Views)

To Those That Understand Javascript. / Pidgin English Tutorial: Understand Javascript Promise 1 / Understanding Closures In Javascript (2) (3) (4)

(1) (Reply) (Go Down)

Understand Javascript Closures by pidginedtec(m): 5:41am On Aug 05, 2020
A closure is a function that remembers its outer variables and can access them.

In JavaScript, closures are created every time a function is created.

To see it in action, watch this video:


https://www.youtube.com/watch?v=otNkXqcihpA
Re: Understand Javascript Closures by engrjoshua: 12:50pm On Aug 05, 2020
Kudos.
Love the concept of pidgin. We are Nigerians

Keep it up broda

1 Like

Re: Understand Javascript Closures by spartan117(m): 5:37pm On Aug 05, 2020
I have a question concerning closures.

I wrote this function expression recently (first image), i expected it to return the inner function but it returned NaN.

So i changed it to a function declaration (second image) and it returned the inner function just as i wanted.

So my question is why did both functions behave differently with regards to closures. I know that there are little differences between function expressions and function declarations. Thanks

Re: Understand Javascript Closures by modash(m): 5:52pm On Aug 05, 2020
@spartan117
The issue is the one is an arrow function and the other isnt and arrow functions. Keeping in mind that arrow functions dont have the arguements keyword

3 Likes

Re: Understand Javascript Closures by pidginedtec(m): 5:58pm On Aug 05, 2020
modash:
@spartan117
The issue is the one is an arrow function and the other isnt and arrow functions. Keeping in mind that arrow functions dont have the arguements keyword

Spot on!
Re: Understand Javascript Closures by pidginedtec(m): 6:00pm On Aug 05, 2020
modash:
@spartan117
The issue is the one is an arrow function and the other isnt and arrow functions. Keeping in mind that arrow functions dont have the arguements keyword

In addition:

Arrow functions do not have arguments binding. But they have access to the arguments object of the closest non-arrow parent function.
Re: Understand Javascript Closures by pidginedtec(m): 6:02pm On Aug 05, 2020
engrjoshua:
Kudos.
Love the concept of pidgin. We are Nigerians

Keep it up broda


Thank you, I hail!
Re: Understand Javascript Closures by spartan117(m): 7:19pm On Aug 05, 2020
modash:
@spartan117
The issue is the one is an arrow function and the other isnt and arrow functions. Keeping in mind that arrow functions dont have the arguements keyword
Ah I see!
Thanks for this
Re: Understand Javascript Closures by spartan117(m): 7:21pm On Aug 05, 2020
pidginedtec:


In addition:

Arrow functions do not have an arguments binding. But they have access to the arguments object of the closest non-arrow parent function.
Now i understand better.
Thanks man

1 Like

(1) (Reply)

What Is The Best Nairaland Andriod App? / Are Social Media Startups That Hard To Run. / Erlang Programming Language

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.