My question is basically an extension to this one: var functionName = function() {} vs function functionName() {}. I've read through all the answers, but I think I'm still missing the answer to this question:
Why does the type of function declaration matter for jQuery's onComplete event and the native setInterval method? Moreover, why does it matter for these, but not for the normal addEventListener?
Example with onComplete events (assuming blinkOn()
is called):
//like this the onComplete event works normally and the functions call eachother
function blinkOn() { $blink.fadeIn( 500, blinkOff ); }
function blinkOff() { $blink.fadeOut( 500, blinkOn ); }
//like this blinkOff won't be called after the fadeIn
function blinkOn() { $blink.fadeIn( 500, blinkOff ); }
var blinkOff = function() { $blink.fadeOut( 500, blinkOn ); };
Example with setInterval:
//works fine
var timer = setInterval( onTimer, 700);
function onTimer() { ... }
//won't work
var timer = setInterval( onTimer, 700);
var onTimer = function() { ... }
The reason I'd like to know is because I want consistency in my code. If the only way to get that is just using function a() { ... }
, that's okay. But I think var a = function() { ... }
is more clear in defining scope. And I'm just really curious.