Or put another way, why does semicolon insertion fail, leaving the code below broken.
function Foo() { }
Foo.prototype.bar = function () {
console.log("bar");
} // <------------ missing semicolon
(function () {
Foo.prototype.la = function () {
console.log("la");
};
})();
Why is the JavaScript parsing engine trying to combine Foo.prototype.bar = function () { with what’s in my closure? Is there anything I could put in that closure that would make this sensible?
I’m not advocating leaving off semicolons with the expectation that semicolon insertion will save you; I’m just wondering why (a more useful version of) the above code broke when I accidentally left one off.
Think of it like this…
I don’t like using
()for IIFE. I prefer other operators.If we go back to the original, and have the first function return a function, you’ll see that one invoked.
Updated to use a unary operator as suggested by @Lasse Reichstein, as a binary operator will still evaluate its left and right operands, and return the result, which will be used for the assignment.