Live data from Hacker News

A re-introduction to JavaScript

developer.mozilla.org

21–30 of 115 posts

Re: A re-introduction to JavaScript

#21

Very nice to see it starts off with a (correct) discussion of JS types! JS devs mostly don't know the actual types in JS, which is pretty crazy if you think about it.

I think 99% of the problems people have with Javascript are not actually types, but `this`. It's the most confusing part about Javascript. This context, var foo scope, what? Why is this value not updating? undefined? What???

http://bjorn.tipling.com/all-this

Good stuff here.

Re: A re-introduction to JavaScript

#22
post #7
post #3

https://news.ycombinator.com/item?id=7169513 https://news.ycombinator.com/item?id=3569893 https://news.ycombinator.com/item?id=1524450 https://news.ycombinator.com/item?id=601967

These are all roughly a year apart and it's a very interesting article. I don't really see a big problem with reposts that far apart to catch newcomers' eye.

I've only been here for a couple of months and this was nice to see.

Re: A re-introduction to JavaScript

#23
post #18

The article mentions an idiom for iterating over an array. It says 'an even nicer idiom is...' and then it shows it. I just wanted to say that the behaviour of this 'nicer' idiom may not be what's expected - it stops iterating once it hits the first falsy value. I was quite excited actually when I first saw them idiom, until I quickly realized its limitations

Oh jeez, I just saw that the section you're referring to repeats the old canard about caching the length of an array when iterating over it. Come on, that can't still be good advice. It always struck me as ridiculous to begin with, but almost certainly now it's optimized away.

Not to mention the suggestion you're talking about, which I bet would bite programmers for the reason you mention. Seems like a bad idiom, I've personally never seen it in the wild.

Re: A re-introduction to JavaScript

#24
post #18

The article mentions an idiom for iterating over an array. It says 'an even nicer idiom is...' and then it shows it. I just wanted to say that the behaviour of this 'nicer' idiom may not be what's expected - it stops iterating once it hits the first falsy value. I was quite excited actually when I first saw them idiom, until I quickly realized its limitations

Yeah and not a big fan of them saying you can do for...in on an array while casually mentioning the limitation of doing such. I would consider it far better practice to NEVER iterating over an array with for...in.

Re: A re-introduction to JavaScript

#25
post #20
post #12

> You can also use the unary + operator to convert values to numbers: + "42"; // 42 > [...] However the "+" operator simply converts the string to NaN if there is any invalid character in it. Being a bit pedantic here, why not recommending the Number function which may be less obscure for beginners? Number("42"); // 42

Since calling constructor functions without `new` is generally an error, I'd rather use `parseFloat`.

> Since calling constructor functions without `new` is generally an error

The behavior of Number, Boolean, String, and Array is well-defined, it's safe to call them without new. In fact, in the case of String/Boolean/Number, calling them with new will often do something you don't expect. (Calling them with new gives you a Number/String/Boolean object, not primitive, which can cause trouble when you try to compare them with ===, unless you remember to use their `valueOf` method)

Also, as noted below, it's possible for objects to not have a `toString` method, so attempting to call it to get the object's value as a string could blow up. So it's actually safer to coerce to string by adding an empty string or passing to String().

Re: A re-introduction to JavaScript

#26

"While often derided as a toy" Javascript is not a toy; toys are fun.

What could be more fun than a language that says the result of dividing by 0 is "Infinity"? It's a hoot!!

That's from IEEE 754 (a standard for floating-point arithmetic). `NaN` and -0 exist for the same reason.

Re: A re-introduction to JavaScript

#29

Earlier quoted context omitted.

I think 99% of the problems people have with Javascript are not actually types, but `this`. It's the most confusing part about Javascript. This context, var foo scope, what? Why is this value not updating? undefined? What???

Especially when you need to do something like: var that = this;

Looking at you Ember. Ugh!

    var controller = this;
    
Every time!

Re: A re-introduction to JavaScript

#30
post #18

The article mentions an idiom for iterating over an array. It says 'an even nicer idiom is...' and then it shows it. I just wanted to say that the behaviour of this 'nicer' idiom may not be what's expected - it stops iterating once it hits the first falsy value. I was quite excited actually when I first saw them idiom, until I quickly realized its limitations

Oh jeez, I just saw that the section you're referring to repeats the old canard about caching the length of an array when iterating over it. Come on, that can't still be good advice. It always struck me as ridiculous to begin with, but almost certainly now it's optimized away. Not to mention the suggestion you're talking about, which I bet would bite programmers for the reason you mention. Seems like a bad idiom, I'v…

the problem with using it even when you don't have falsy values is that, eventually, you're going to forget that you can't use it when you do have falsy values. You're going to get too comfortable with it, and it's not general-purpose enough.

Can't wait for ES6 to no longer be 'experimental', as I'd much prefer to use 'for..of' loops

Post reply on HN