Tuesday 19 December 2017

How to understand closures in Javascript?

How can one understand closures in
Javascript?




In
general terms, a closure is a function bound to one or more external variables. When it
is called, the function is able to access these variables. In JavaScript, closures are
often implemented when functions are declared inside another function. The inner
function accesses variables of the parent one, even after the parent function has
terminated




In this
statement, "a closure is a function bound to one or more external variables", does it
mean we can do this : var myFun = Function(msg){...}; is it
correct ?



What does it mean "even after the
parent function has terminated"?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...