A friend of mine posted a short snippet of code on twitter, from something that he saw in another person’s code.
— Cullen Tsering (@CullenTsering) February 2, 2015
Here’s a slightly cleaned up version of this code:
It’s not closures that allow this to work. it is a combination of function scoped variables, and variable hoisting.
By using a
var statement inside of a function, you are using a function-scoped variable, as is show in the example above.
In a browser, if you omit the
var statement, or if you use
var outside of any function, you will attach to the current global scope.
In a nodejs module / file, a variable declared outside of a function will be scoped to the module / file, because NodeJS/CommonJS modules are all wrapped in their own sandbox (function scope). You have to be more explicit to create a global variable in nodejs, and it is recommended you don’t do this.
When you declare a variable inside of a function, the variable declaration is “hoisted” to the top of the function. That is, the runtime will move your variable declaration to the beginning of the function, in spite of where you put the var statement.
Because of this hoisting, it is recommended that you put variable declarations at the top of the function. It will happen for you anyways, so you may as well do this explicitly in order to prevent confusion about the code’s execution.
Rewriting The Function
Here is the equivalent function, making the implicit hoisting in to explicit variable declarations:
This code operates the same at runtime, as the original code from Cullen’s example. The only difference is in the formatting of the code that I’ve used, and in making the implicit variable hoisting explicitly known in the code.
A Matter Of Preference?
I think the variable declaration bit is somewhat a matter of preference. Personally, I don’t declare all my variables at the top of a function (with a few exceptions where I do… but I’m not sure I can explain my exceptions, logically :P)
If you are working on a team that understands variable scope correctly, it shouldn’t matter too much where the variables are declared.
ES6: Block Scope w/ Let
It has been recommended by some, that we all use
let once it becomes standard. Until ES6 comes around, though, be sure to watch your variable declarations.
If you’d like to see the ES6 code in action, check out my short video on using ES6 “let” with IO.js.
- My eBook version of 5 Rules for Mastering “this” ($5)