Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Closures</strong> have already been well handled in Stackoverflow here is just a selection:-</p> <p><a href="https://stackoverflow.com/questions/111102/how-does-a-javascript-closure-work">How does a javascript closure work?</a><br> <a href="https://stackoverflow.com/questions/1801957/what-exactly-does-closure-refer-to-in-javascript">What exactly does “closure” refer to in JavaScript?</a><br> <a href="https://stackoverflow.com/questions/2055641/can-you-say-this-is-a-right-example-of-javascript-closure-where-the-places-we-n">can you say this is a right example of Javascript Closure.. Where the places we need to consider avoiding the closures??</a><br> <a href="https://stackoverflow.com/questions/631187/javascript-scope-and-closure">JavaScript scope and closure</a><br> <a href="https://stackoverflow.com/questions/346015/javascript-closures-and-this-context">Javascript Closures and ‘this’ context</a><br> <a href="https://stackoverflow.com/questions/212401/javascript-how-do-i-learn-about-closures-usage">JavaScript - How do I learn about “closures” usage?</a> </p> <p><strong>Callbacks</strong> are a simpler concept. A callback is basically where a function accepts another function as a parameter. At some point during execution the called function will execute the function passed as a parameter, this is a <em>callback</em>. Quite often the callback actually happens as an asynchronous event, in which case the called function may return without having executed the callback, that may happen later. Here is a common (browser based) example:-</p> <pre><code> function fn() { alert("Hello, World"); } window.setTimeout(fn, 5000); </code></pre> <p>Here the function <code>fn</code> is passed as a <em>callback</em> to the <code>setTimeout</code> function. Set timeout returns immediately however 5 seconds later the function passed as a <em>callback</em> is executed.</p> <p><strong>Closures and callbacks</strong></p> <p>Quite often the reason that closures get created (either incidentally, accidentally or deliberately) is the need to create a callback. For example:-</p> <pre><code> function AlertThisLater(message, timeout) { function fn() { alert(message); } window.setTimeout(fn, timeout); } AlertThisLater("Hello, World!", 5000); </code></pre> <p>(Please read some of the linked posts to grasp closures)</p> <p>A closure is created containing in part the <code>message</code> parameter, <code>fn</code> is executed quite some time after the call to <code>AlertThisLater</code> has returned, yet <code>fn</code> still has access to the original content of <code>message</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload