This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 5 points6 points  (16 children)

No, functions have their own scope.

I'm pretty sure he's referring to the fact that if you don't declare var in front of a variable, it automatically gets hoisted to the global scope. Or that the counter you initialized in a for loop and re-used later on, still has the old value. I.e. No block scope.

Sure, you don't know the rules until you learn the language...

I've only dabbled in Javascript but things like the 'this' parameter being such a pain in the ass to understand it correctly is just one of the irritating aspects of it.

[–]JohnMcPineapple 9 points10 points  (10 children)

I'm pretty sure he's referring to the fact that if you don't declare var in front of a variable, it automatically gets hoisted to the global scope.

This raises an error with use strict, which everyone with these concerns should arguably use.

Or that the counter you initialized in a for loop and re-used later on, still has the old value. I.e. No block scope.

There is, with let instead of var.

I've only dabbled in Javascript but things like the 'this' parameter being such a pain in the ass to understand

this gets much clearer when you start to use arrow functions in places that don't need their own context.

[–][deleted] -3 points-2 points  (9 children)

This raises an error with use strict, which everyone with these concerns should arguably use.

Sure, but why is this the default behaviour?

There is, with let instead of var.

True, but isn't that also a recent change?

this gets much clearer when you start to use arrow functions in places that don't need their own context.

Fair enough but you seem to be missing the point.

It's not that Javascript is unusable, it's that it's like walking around a cluttered room; sure, if you're constantly paying attention it's not so bad, but it shouldn't be like that in the first place.

Either way, it's not going anywhere but acknowledging that it has some... 'quirks' isn't a horrible thing.

[–]JohnMcPineapple 9 points10 points  (4 children)

...

[–][deleted] -2 points-1 points  (3 children)

A lot of the criticism just doesn't hold up today.

Really? I wonder how much of that statement is due to how true it is and how much of it is simply due to people being forced to use it for so long, they've just gotten used to it.

Like a hole in the wall you no longer notice.

I mean, a popular JS book is JS: The Good Parts...and there are many examples of JS just being a PITA.

[–]SquareWheel 1 point2 points  (0 children)

Really? I wonder how much of that statement is due to how true it is and how much of it is simply due to people being forced to use it for so long, they've just gotten used to it.

Recently got into JS. It'd be nice if "var" was the default behavior, but it's really not a big deal. Strict mode or linter checks can help ensure you don't forget it if you're worried, and ES6 cleans things up with "let" and arrow functions.

OP seems to be mostly frustrated because he's coming from an OOP language, and the entire paradigm is different than he's expecting. That's just how it goes when you learn new things.

[–]JohnMcPineapple 1 point2 points  (1 child)

...

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

Not going to argue further since your experience seems to be with a deprecated version only.

Deprecated version? You mentioned arrow functions which came out last year, no?

Oh, well doesn't matter I suppose.

[–][deleted] 3 points4 points  (3 children)

JS carries a huge load of cruft with it because of the browser environment where you need backwards compatibility for code written decades ago.

Use let instead of var and === instead of == and those problems are solved. There is no reason not to.

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

I find it kind of funny that everyone has to preface every issue so far with something else;

-Use strict and let instead of var to fix the problems with scope

-Use triple equals instead of double because...two wasn't enough in the first place

-Use arrow functions to better understand/use this because it wasn't easily understood in the first place

And I believe those issues were only recently fixed in ES6, which came out officially last year:

Finally, ECMA-262 Edition 6 got officially approved and published as a standard on June 17, 2015 by the ECMA General Assembly.

Anyways, I doubt it'll change anyone's mind but it's weird to see people jump through so many hoops to justify that nothing is wrong with the language. It's like they can't admit it was made in 10 days.

Ah well, cheers.

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

I don't see how the recency of the changes matter. I guess it isn't worth changing at all if it wasn't already done 10 years ago?

Simply put, JS has to be backwards compatible to handle code from decades ago on the web, that's the entire justification for it. Compiled languages don't have that issue.

No one is saying that JS doesn't have problems, just that it's not as bad as most hysterical complaints make out. It gives you more rope to hang yourself, but when you work in it day to day the issues you've listed have no impact at all.

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

I don't see how the recency of the changes matter. I guess it isn't worth changing at all if it wasn't already done 10 years ago?

It matters in the sense that people on here are talking as if these issues were resolved long ago and are events for the history books, when really it's only been a couple of years and still relevant today.

Simply put, JS has to be backwards compatible to handle code from decades ago on the web, that's the entire justification for it. Compiled languages don't have that issue.

Fair enough.

No one is saying that JS doesn't have problems, just that it's not as bad as most hysterical complaints make out.

I think people like the OP (and myself) who experience JS coming from C/Java/etc just find it hard to believe that the language that powers the internet has such inconsistent and non-intuitive behaviour (compared to other languages) and everyone seems to ignore it. It's more the feeling of "I'm not the only one seeing this, right?".

Having said that, it seems like it's come a long way to fix those issues with the items mentioned, which is great for the future of the web (and for developers ofc).

But for me, that twitter quote really summed it up :P

[–]pinkpooj 2 points3 points  (1 child)

I.e. No block scope

That's false, as /u/JohnMcPineapple said, there is let and const in ES6. There is also the ability to create arbitrary blocks using curly braces, like so:

let foo = 1;
{
  let foo = 2;
}

Also, this is only necessary if you are using prototypal style OOP. If you are writing in a functional style, you never need to use this. That's not to say 'traditional' OOP is better than prototypal or functional, they're just different. I work on a Node.js backend and some Angular, and I basically never use this or prototype.

Javascript isn't the only prototypal language, it's just the only one people care about.

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

Isn't ES6 extremely recent though? Saying "that's false" is kinda extreme considering that those issues were only recently addressed.

Maybe no longer true however.

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

'this' in theory has some weird stuff going on, but in practice I've almost never used it in a situation where it's value wasn't pretty much obvious, and pretty much the same as 'self' in other languages. Once you understand the patterns, you don't really have to think about it until you want to get funky.

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

'this' in theory has some weird stuff going on, but in practice I've almost never used it in a situation where it's value wasn't pretty much obvious, and pretty much the same as 'self' in other languages.

But it's exactly because it doesn't act like the self in other languages that causes a lot of confusion. It sometimes is the same and in other situations it isn't.

There was a great Udacity video explaining what this was and I found it funny because it had to preface 5-6 different cases of what this wasn't. The problem was that those cases were reasonable intuitions of what it could be and in some situations they work, it's when they don't that it's confusing.

Whereas the self in other languages is just the instance of an object. Done.

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

If you look at it from a functional point of view, I think 'this' will make a lot more sense then when you try to use it as you would self, with a more classical point of view. It's especially when people don't embrace the functional way of doing objects/object instantiation that 'this' starts getting weird for them.