I do not want to work in environment where people feel that it is appropriate to routinely "punish" colleges by making them waste time like this.
But you gotta admit, if you're on the pranking end, it could be fun to see someone tear their hair out.
Or my sense of humour is way out of whack. probably the latter
function foo() {
bar = function(){}; //define bar
}
function baz() {
bar(); // use bar
}
Note that both foo() and baz() could themselves, be inside another function. Doesn't really matter. Now `bar` is a defined as a a property of the global object.If you add
if (false) {
var bar;
}
in baz(), most developers, as I had noticed, will ignore it - afterall, it's a false condition, the code will never enter that branch. But because variables are "hoisted", you have now declared `bar`, it's lexically scoped as a new variable under `baz()`, with the value `undefined`.So, when `bar()` gets called, it becomes an error, because `undefined` is not a function definition.
...
and yes, dear JavaScript developers, despite all the good efforts of people like Nicholas Zakas, Douglas Crockford, Addy Osmani and the like, bad JavaScript still exists in the wild. And I feel, in greater numbers than ever, and I am not optimistic that the amount of bad JavaScript will be ever reduced. This depresses me. Can't be helped, I guess.
By making the global variable local to the scope around the if statement, side-effects won't escape the local scope (as said, the if block does not declare a new scope).
It's fun (in a harmless fun kinda way, but you can also use some really dirty tricks to really hamper someone's work) to fuck up people's code - that's why I wrote Underhanded JavaScript. :P
EDIT:
Another fun one is to add `return`s to constructor functions.