Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 18, 2021 03:20 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To