6 ms·
What is the point of using "var" declaration in your language?
by tszming 15y ago
What is the point of using "var" declaration in your language?
- qixxiq 15y agoIt enables javascript style scoping which I am a big fan of. Basically the 'var' statement defines the depth that a variable is available at.
- tszming 15y agoI am just curious because the Global Variables and Scope are the Awful Parts of JavaScript: http://oreilly.com/javascript/excerpts/javascript-good-parts/awful-parts.html http://oreilly.com/javascript/excerpts/javascript-good-parts...
- dchest 15y agoHaving `var` keyword doesn't imply automatic assigning to undeclared globals.
- tikhonj 15y agoHaving var implies nothing about automatically making variables global. If you're familiar with Scheme, having var is like define and not having it is like set!. (If you're not familiar with Scheme, you should be ;). Go read SICP.) Basically, having something like var lets you nest scopes. So you can have, say, nested functions and work with variables declared above your current fucntion but not in the global scope. If you leave out the var, you'll end up with Python's idiotic scoping, which is annoying. (They've added a nonlocal keyword in the new version in an attempt to fix it, but it's still relatively clumsy and awkward.) Coincidentally, if you use the strict mode in JavaScript (with "use strict";) it will prevent you from using global variables implicitly. This makes it even more clear that var and JavaScript behavior regarding global variables are orthogonal.