JavaScript block scope vs function -


are following snippets equal? if no deference?

var x = (function() {     ... //a     return function(){         ... //b     }; })(); 

vs.

var x; {     ... //a     x = function(){         ... //b     }; } 

there major difference: in javascript, blocks don't induce new variable scope. therefore, can't define private variables in // a code block. compare

var x = (function() {     var v = 42;     return function(){         return v;     }; })(); // v; yield referenceerror: v not defined, need call x 

and

var x; {     var v = 42;     x = function(){         return v;     }; } // v 42 here, that's not what's intended. 

Comments