You need to enable JavaScript to see this example.
Consider this code, particularly the variable callback
and the line highlighted in bold:
function firstCallback(responseText) {
p = document.createElement("p");
p.appendChild(document.createTextNode("First callback got response: " + responseText));
feedback.appendChild(p);
callback = secondCallback;
}
function secondCallback(responseText) {
p = document.createElement("p");
p.appendChild(document.createTextNode("Second callback got response: " + responseText));
feedback.appendChild(p);
callback = firstCallback;
}
var callback = firstCallback;
The first time the callback is invoked using callback()
, the value of
the variable callback
is a reference to the function firstCallback
;
the last act of that function is to assign a reference to the function secondCallback
to the callback
variable. Thus, when another call is made using
callback()
, the second function is executed instead.