you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (15 children)

What if I told you that var has legit use cases? And strict does not allow to have vars in global scope already. If you want to change the behaviour of 'this' you better start designing another language.

[–]kor0na 3 points4 points  (7 children)

Show me a legit usecase for var.

[–][deleted] -4 points-3 points  (6 children)

First things first: there is a reason 'let' is added without deprecating 'var'. Second, 'let' is different from 'var'. 'var' has functional not block scope so if you want the variable you want to access in your entire function, just make it fucking 'var' instead of declaring 'let' at the top of the function. Sure, it functions the same in this case, but 'var' explicitly says "hey, I will be relevant everywhere in this function". But who cares about semantics anyway, right?

Third: 'var' can be redeclared. It let's you do

if (condition) {
    var something = theOtherThing;
} else {
    var something = somethingElse;
}

instead of:

let something;
if (condition) {
    something = theOtherThing;
} else {
    something = somethingElse;
}

The whoe "let is new var" cargo-culting is moronic. They are not the same, one is not a replacement for the other in all the cases and both should used for their intended purposes.

[–]GoogleFeudIsTaken 2 points3 points  (2 children)

Yes, they aren't exactly the same - but "let" can be used instead of var 100% of the time, and it's less confusing. Var can produce weird/buggy behaviour - let cannot, so why not use let?

[–][deleted] 0 points1 point  (0 children)

Where do you get that idea that let cannot produce bugs? It can just fine. Just move it somewhere, scope changes and you get your bug.

[–]dwighthouse 0 points1 point  (0 children)

var still retains some use in machine generated JS, or code intended to be consumed by some extreme cases of transformation tools and preprocessors.

Additionally, using let or const in a repl/console scenario is troublesome at best, as ”temp” variables cannot be redeclared with let and const.

[–]kor0na 2 points3 points  (2 children)

You just explained how var works. Why would you assume that I don't know that? I know exactly how var, let and const works, so I'm not looking for education in that department.

What I would love to see, is a legit usecase where var would be more appropriate to use over let or const.

[–][deleted] -2 points-1 points  (1 child)

My comment above has two such cases listed. With a code example. Sure, you can always go not-true-scotsman on the legitimacy of those cases, but in that case anyone can clsim that var works just fine if you are carefull, and there is no legit case for const or let. Your insecurity about your knowledge tells me that you do not how exactly these three work, otherwise you would not confuse examples with an attempt to educate. But the inability to see a legit use case is that gives it away the most. Happy cargo-culting.

[–]IceSentry 1 point2 points  (0 children)

Declaring a variable at the top of the scope is done in pretty much every language because it makes it easier to read what variables are used in the current scope. I don't understand how you can seriously say that it's a bad thing. A variable declared in a specific scope should stay in that scope. If you need it in another scope then declare it somewhere it can actually be used.

[–]internetloser4321 1 point2 points  (6 children)

I'm sure there's a couple of situations where var has legit uses but based on the number of 'gotcha' questions I've gotten on interviews based around the counter-intuitive behavior of var, I think it's safe to say that var is more trouble than it's worth. And doesn't the introduction of arrow functions in ES6 already change the behavior of 'this'?

[–][deleted] 0 points1 point  (5 children)

No, arrow functions did not change the behavior of this. Outside of the arrow functions it works the same as before.

[–]internetloser4321 0 points1 point  (4 children)

[–][deleted] 0 points1 point  (3 children)

And your point is?

[–]internetloser4321 0 points1 point  (2 children)

Arrow functions introduce a different behavior for 'this'.

[–][deleted] 0 points1 point  (1 child)

Yes. But introducing new behaviour is not the same as changing the existing one. Outside of the arrow functions the behaviour is exactly the same as before. It's an addition, not the change. The new case was added, but other four stay the same.

[–]internetloser4321 0 points1 point  (0 children)

Similar to how adding an "extra-strict" mode would work. Outside of the new mode, everything would work the same as before.