I’m working on a short JavaScript program, which works perfectly fine ONLY if I inject any trivial statement–such as var asd;–at a specific location. The program is intended to demonstrate a simple encapsulation technique.
No other locations work. ANY trivial statement works.
This is not a DOM load issue since I’m not even dealing with the DOM.
version that works: (warning, 3 alerts)
http://jsfiddle.net/bZUm6/3/
version that does not work:
http://jsfiddle.net/bZUm6/2/
Please note the “var asd;” in the first version.
Can someone please tell me why?
I would really appreciate it.
Max
It’s the semicolon removal that’s breaking it.
It will work even if you change…
simply to this…
The reason is that the next line of code starts with
(, which happens to be wrapping a function with its closing).This is being interpreted as a function call operator, and is attempting to invoke the previous expression.