what difference does it make:
function a(){
//...
}
setInterval(a, 1000);
vs
setInterval(function(){
a();
}, 1000);
Except of the obvious that I can pass parameter in the second case.
It should not necessarily be setInterval. Any function that can accept function handler.
Passing an anonymous function also allows you to call
someObject.a()and preservethis.If you write
setTimeout(someObject.a, 300), thea()method will be called in the context of the global object, so that itsthiswould bewindow. If it expectsthisto besomeObject, it will break.If you write
setTimeout(function() { someObject.a(); }, 300),a()will be called in the correct context.