JavaScript Closures
Closures are functions that refer to variables from their parent environment. To understand them better, let's take a look at the following example:
function parent() { const message = 'Hello World'; function child() { alert (message); } child(); } parent();
In the preceding example, you can see how the child()
function has access to a constant defined in the parent()
function. However, this is a simple example, so let's look at a more interesting one:
function parent() { const message = 'Hello World'; function child() { alert (message); } return child; } const childFN = parent(); childFN();
This time, the parent()
function returned the child()
function, and the child()
function is called after the parent()
function has already ...
Get MEAN Web Development - Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.