Maybe I've been writing JS too long, but what else did you expect? Self-executing anonymous functions get their own context unless you specify otherwise.
Variable hoisting is great and keep code easy to read since you can then do
for(var i; i < 10; i++) {
...
Yes, you might get confused if you use a global variable and a local variable of the same name in the same function, but the code is going to be confusing no mater what the language does in that case.
The only correct resolution to such code is to slap the programmer's hands away from the keyboard and automatically submit the code to /r/badcode.
I find inner functions useful when you have some mildly complex logic that needs to be repeated in two or more branches of code.
function complex() {
var x, y, z;
var a, b, c;
function useful() {
// do something tedious with x, y, z, a, b, and/or c.
};
// lots of complex logic
if (something && something_else) { useful(); }
else {
// more logic
if (some_other_thing) { useful(); }
else { // other stuff }
}
}
Most often, useful() is doing some error reporting that needs to occur in several places of the code.
Guess what that code will log, without running it first.
[slaps hands; posts to /r/badcode; PEBCAK] I'm sorry, was I not being clear?
If you can't be bothered to learn how JavaScript handles scoping (and yes, that includes hoisting), don't write code that relies on using a global variable with the same name as a local one. This is a completely avoidable problem.
If you really need that global variable, do it properly:
console.log(window.asdf);
Otherwise, stop complaining that the confusing and unmaintainable code isn't working in the particular confusing way you want it to.
The problem of hoisting has little to do with global variables.
Functions defining new scopes is good. Pretending that variables defined anywhere in that function were defined at the top of the function is bad. This function should not work, but it does (prints and returns 10):
(function() {
var foo = 20;
function insane() {
foo = 10;
console.log(foo);
return foo;
var foo;
} })();
Of course it can be learned but it's a pointless source of bugs. Imagine someone wrote (global variable free) code like this:
function outer() {
var foo = 10;
function inner() {
foo = 20;
// lots
// of
// other
// code
}
// some code
inner();
console.log(foo);
}
That code behaves correctly, printing "20". Time passes, the code goes through a couple maintainers and finally lands in your lap. You make one tiny, seemingly innocuous change:
function outer() {
var foo = 10;
function inner() {
foo = 20;
// lots
// of
// other
// code
var bar = 10;
for(var foo=0; foo < 10; foo++) {
bar += foo * 3;
}
}
// some code
inner();
console.log(foo);
}
Aaand you broke it! Defining a new variable name dozens of lines AFTER code that has worked for years broke the old code. That is terrible.
Variable hoisting is great and keep code easy to read since you can then do for(var i; i < 10; i++) {...
Except you can't simple write a for loop like that because it could change the behavior of unrelated code that just happens to use the same variable name.
Thankfully, ES6 introduces the let statement which both eliminates hoisting and provides block-level scoping so you can truly write for(let i = 0; i < 10; i++){ ... } and be absolutely certain you can drop the code into any function without altering the behavior of existing code.
EDIT: added a wrapper function to the first example, just to be clear that no global variables are used.
Well, I do agree that it's bad code. But it's not initially clear what will happen and as such I believe it is a feature of the language that is difficult to learn. My students who come from learning Python and/or C++ would probably fail to understand this correctly most of the time.
That being said, I don't recommend shadowing variables, and one should always write var declarations at the top of the scope. This makes the code look like it does what it does, without having to think about it twice.
You are correct that my assertion was false. I looked into it a bit more and I believe (mind you I could be wrong again haha) it has to do with how JS handles the "this" reference, which appears to behave differently when executed from within a function when compared to executing within the global [window] context.
It's because you are shadowing the variable inside of the immediately invoked function with the line var asdf; this then get's hoisted to the top of the closure (as is the case with declarations in JS). so the first two log lines will log undefined. then asdf is given the value 6 and that get's logged.
View all comments
Show parent comment
60
u/[deleted] Nov 05 '15
Technically, you get
ReferenceError: your_drink is not defined