javascript - Why is this grouping operator + function immediately invoked -
i'am studying behaviour of immediatly invoked function expressions (iife) , while doing encounterd next situation.
(function () { document.write("bar"); }) (function () { document.write("foo"); }());
i thought first grouping operator function look within without calling it. sec grouping operator function look phone call of function.
what find unusual both invoked, why that?
(function () { document.write("bar"); }) var x = 1; (function () { document.write("foo"); }());
when break 2 inserting variable declaration in between, it's writes foo. expected.
because forgot semicolon after first function expression:
(function () { document.write("bar"); });
otherwise sec "grouping operator" interpreted function call. this:
(function a() { ... }) (function b() { ... }());
is same as:
function b() { ... } (function a() { ... })(b());
reordering makes bit easier see. remember white space characters have no meaning in javascript , ignored.
javascript iife
No comments:
Post a Comment