3 ms·
Sure, forgetting the var causing a global variable is a problem, and I'd have no problem with removing variable hoisting and requiring all vars to be declared a
by seasoup 12y ago
Sure, forgetting the var causing a global variable is a problem, and I'd have no problem with removing variable hoisting and requiring all vars to be declared at the top of the function, it's what I do anyway.
But adding a block level variable declaration just adds to the complexity of the language and doesn't add any real additional capabilities. It adds some nice fluff around loops and asynch operations, but that is all easily done using current scoping rules once you understand closures.
var is still going to be around, but now we've added let. you might like let's scoping rules better then var's scoping rules, but now we'll have both. the interaction between the two is going to have some amazingly confusing and complex edge cases. If you think that hoisting and closures are confusing now, just wait until let is thrown into the mix as well. Even if let is simpler, var isn't going away. So we're just adding complexity to the system.
- olavk 12y agoI don't really see any edge cases in the interaction between let and var, can you provide an example? But yeah, using both var and let in the same function would be ugly. Probably you would want to stick with let in new code since there is no benefit to var over let. But obviously var has to remain in the language for backwards compatibility.
- seasoup 12y agoI haven't done a deep study on let to know how it interacts with var. Maybe it's something simple and I don't know it yet but how does this work: var foo = "abc"; function do ( foo ) { foo = "def"; console.log( foo ); let foo = "bar"; console.log( foo ); var foo = "baz"; console.log( foo ); } console.log( foo ); do( foo ); console.log( foo ); And of course var has to be around for backwards compatibility, but if the addition of let is being made to make variable declaration more intuitive or simpler then it is failing because var must be included. You cannot consider let in isolation and when considered in combination with var, it makes things more complicated.
- olavk 12y agoI would assume you get a compile-time error because you are declaring 'foo' twice. I guess you are right that the language is getting more complex because you are introducing a new way of declaring variables. However the added complexity in the language means you can write code which is less complex and error prone (if you consistently use 'let' instead of 'var'), and I think this is a fair trade-off given that backwards compatibility must be preserved.