An Interest In:
Web News this Week
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
October 18, 2021 03:20 pm GMT
Original Link: https://dev.to/mohmmadalhallaq/simple-example-of-closures-in-javascript-4f3h
simple example of Closures in JavaScript
Closures are a fundamental and powerful property of Javascript
//we have an outer function named walk and an inner function named flyfunction walk (){ var dist = '1780 feet'; function fly(){ console.log('At '+dist); } return fly;}var flyFunc = walk(); //calling walk returns the fly function which is being assigned to flyFunc//you would expect that once the walk function above is run//you would think that JavaScript has gotten rid of the 'dist' varflyFunc(); //Logs out 'At 1780 feet'//but you still can use the function as above //this is the power of closures
Original Link: https://dev.to/mohmmadalhallaq/simple-example-of-closures-in-javascript-4f3h
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To