4 ms·
I'm learning the variable scope of javascript very thoroughly because it is powerful. I don't want to be hindered by shortcuts eliminating some of that power/un
by JimmaDaRustla 14y ago
I'm learning the variable scope of javascript very thoroughly because it is powerful. I don't want to be hindered by shortcuts eliminating some of that power/understanding.
- tluyben2 14y agoWithout trying in jsfiddle, what does this alert; var i=3; function x() { alert(i); var i=5; } x(); and why is this powerful? I agree with you mostly, but I don't like this 'feature'. Edit: hoisting yes, but why, besides compiler optimization is it more powerful for the programmer than if it would not do hoisting, or, why is it in a modern language like JS? Edit1: I'm not in favor of coffeescript I used Javascript most of the time during my work day, I was just seriously wondering, if not for low level optimization why it would be a problem to have global scope until local is defined.
- deelowe 14y agoundefined, of course. What's confusing about it? The function hoisting?
- tluyben2 14y agoThe hoisting yes. I didn't say it's confusing. I said I don't like it. And more particular I don't see why it's powerful and why the logical result (being that it takes the global i until the local is defined) would be less powerful.
- grannyg00se 14y agoJavascript is great if you don't do oddball things with it. Nobody can rightly deny that there is a ton of potential to do stupid things with javascript. I'm sure someone could write out a line of [other language] code that is quite the head scratcher, and works as defined in the spec. If coffeescript works as a nice complete syntax re-do for you, that's good but you have to learn two programming languages in that case. I prefer raw js, auto-linted to help highlight any potential stupidity. In the supplied example, the var at the end of the function would be caught by the linter indicating that it leads to bugs and there is no good reason to do it - so don't.
- JimmaDaRustla 14y agoUndefined, that's hoisting. I think it is dirty, but the point of the var statement (and your hoisting example) is that you implicitly imply the variable scope. In Coffeescript, you just do an assignment and it will be function scope if it (the variable) doesn't exist in any parent scopes? That seems dirtier to me and dangerous. If you are awesome and namespacing and keeping clean/modular/maintainable code, Coffeescript may work great.
- JimmaDaRustla 14y agoSomeone correct me if I'm wrong, because I'm not a pro by any means: var statement allows for implied variable scope and avoid variable collisions from parent scopes