Understand Javascript Closures - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Understand Javascript Closures (567 Views)
| Understand Javascript Closures by pidginedtec(op): 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 |
| 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 |
| Re: Understand Javascript Closures by pidginedtec(op): 5:58pm On Aug 05, 2020 |
modash:Spot on! |
| Re: Understand Javascript Closures by pidginedtec(op): 6:00pm On Aug 05, 2020*. Modified: 8:23am On Nov 18, 2020 |
modash: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(op): 6:02pm On Aug 05, 2020 |
engrjoshua:Thank you, I hail! |
| Re: Understand Javascript Closures by spartan117(m): 7:19pm On Aug 05, 2020 |
modash:Ah I see! Thanks for this |
| Re: Understand Javascript Closures by spartan117(m): 7:21pm On Aug 05, 2020 |
pidginedtec:Now i understand better. Thanks man |
To Those That Understand Javascript. • Pidgin English Tutorial: Understand Javascript Promise 1 • Understanding Closures In Javascript • 2 • 3 • 4
Learn To Code And Live A Better Life: Become A Mobile App Developer • How To Create A Website For Free? Get Free Hosting And Domain Using A Smartphone • I Need A PHP Developer To Work As Full Time Staff But Remotely