I have two versions of the code. Can someone let me know which is optimal and faster?
Version 1:
function showMsg(a)
{
alert(a);
}
function invokeShowMsg()
{
var msg = 'hi';
showMsg(msg);
}
window.setTimeout(invokeShowMsg,1000);
Version 2:
function showMsg(a)
{
alert(a);
}
window.setTimeout(function(){showMsg('hi');},1000);
One more doubt, is the Version 2 way of calling called “Closure”?
As far as speed goes, you will not notice any difference between the two whatsoever, so pick what you like.
I prefer #2, as it is cleaner and keeps the syntax readable: