Reputation: 17184
What difference does it make to do the following:
function (callback) {
var callback = callback || false;
combineCallback = function () {
callback.apply(window);
....
}
getJSON(combineCallback);
}
Or with this:
function (callback) {
var combineCallback = function () {
callback.apply(window);
....
}
getJSON(combineCallback);
}
Does it make any difference to write write var callback = callback || false;
?
Upvotes: 1
Views: 55
Reputation: 7719
var
will not "shadow" a local variable in the same scope. Likewise, var
will not "shadow" a parameter (which is a local variable/binding itself). Simply, each time the function is invoked, callback
represents a different local variable which initially contains the first argument passed; and there is only one local variable called callback
.
Because var doesn't "create" a new variable (search for "hoisting"),
function (callback) {
var callback = callback || false;
and
function (callback) {
callback = callback || false;
are equivalent - no difference. (Although I find the latter more clear.)
However, removing the callback = callback || false
changes the semantics. In particular, callback may end up with false-y values like 0 without that line. Whether or not this is needed/useful here is a different issue as (false).apply(..)
will still result in an error.
Here is a simple TTL for x || y
:
x y x || y
------- --- ------
TRUTH-y ANY x
FALSE-y ANY y
Upvotes: 1