Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;?

share|improve this question
    
if the function is called more than once.. will it be overright with previous callback if i don't define var? –  Basit May 31 '13 at 6:22
    
false.apply() and undefined.apply() throw a similar error, so in that regard there is no difference. –  Juhana May 31 '13 at 6:24
add comment

1 Answer

up vote 1 down vote accepted

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
share|improve this answer
    
I think that the OP's question is if including var callback = callback || false; in the code will make a difference or not. –  harsha May 31 '13 at 6:23
    
you edited the answer milliseconds before I posted the comment.Sorry! –  harsha May 31 '13 at 6:24
    
f the function is called more than once.. will it be overright with previous callback if i don't define var? – –  Basit May 31 '13 at 6:25
    
@Basit Have you tried it? What happens when you do? –  Juhana May 31 '13 at 6:25
1  
@Basit The problem with the code in the linked question is it doesn't use var. It would awful if parameters "leaked" scope or were "promoted" to global variables! That would lead to so many bugs that could not help but be avoided! Anyway, the var in this case is completely irrelevant to how the code will run. –  user2246674 May 31 '13 at 6:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.