Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>callback &amp;&amp; function() {callback.call(...)};</code> is doing two things:</p> <ul> <li>testing whether a <code>callback</code> has been defined and</li> <li>if it has, calling it in the correct context</li> </ul> <p>JavaScript has a feature called <em>"short-circuiting logical expressions"</em>. If a part of a logical expression cannot change the overall result, it is not going to be evaluated.</p> <p>If <code>callback</code> is <code>undefined</code>, then it evaluates to false in the context of a logical expression (it's <em>"falsy"</em>). So the expression becomes the equivalent of <code>false &amp;&amp; ...</code>, in which case it does not matter anymore what <code>...</code> actually is, the result will always be false <sup>†1</sup>. JavaScript is not looking at the <code>...</code> in this case.</p> <p>If <code>callback</code> is <em>not</em> <code>undefined</code> (but a function), the first part of the logical expression evaluates to true (it's <em>"truthy"</em>). At this point JavaScript evaluates the <code>...</code>. The overall result happens to be a function expression. The function expression results in an anonymous function, which is then passed to jQuery's <code>animate()</code>.</p> <p>The <code>callback.call()</code> executes <code>callback</code> <em>and</em> determines the meaning of <code>this</code> within <code>callback</code>. So <code>callback.call(this)</code> makes sure that <code>this</code> refers to the same object as in the outer function.</p> <hr> <p><sup>†1</sup> <em>To be absolutely correct: The overall result of the expression will not be <code>false</code>, it will be <code>undefined</code>.</em></p>
 

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