Live data from Hacker News

JavaScript fundamentals before learning React

robinwieruch.de

51–60 of 72 posts

Re: JavaScript fundamentals before learning React

#51
post #6

Earlier quoted context omitted.

Hi, Robin. Then you may want to say that `const` guarantees that the name will remain bound to that object. The great-GP is correct in that `const` has nothing to do with immutability besides that. It doesn't necessarily conveys an intent of inner immutability. It just states that whatever is in the variable will stay there until it goes out of scope. Using `const` whenever possible is good advice, off course.

It's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but gloss…

I tend to go by the following rules:

Use const by default. If I need let, see if I can refactor to using const.

I've found that actually I can go far without using let, improving my code along the way.

I see a pattern from colleagues. They initialise a variable using let and then branch to figure out what should go inside it. That can be replaced by a pure function.

For loops tend to be the next thing. In that case, they can be replaced by higher order functions/methods without detriment to clarity (usually an improvement).

Perhaps the move towards iterators, with async support, will make it more common and legitimate. I haven't yet used those in a codebase but perhaps let in that instance leads to clearer code.

Re: JavaScript fundamentals before learning React

#53
post #34

Earlier quoted context omitted.

It's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but gloss…

maybe it’s because const is 2 more letters to type. if “let” was “letitbe” I bet const would be more popular seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.

> I wish they could have chosen a 3-letter word for const in keeping with var and let.

"set" or "fix" would be quite nice, given what const actually does.

Re: JavaScript fundamentals before learning React

#54
post #34

Earlier quoted context omitted.

It's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but gloss…

maybe it’s because const is 2 more letters to type. if “let” was “letitbe” I bet const would be more popular seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.

It would have been nice to have `let` work like `const` does, and something else for what is currently `let` (`mut`?).

The TC39 was constrained by the list of reserved words though. `let` and `const` were reserved in ES5, and there was nothing that could have conveyed `mut`.

https://mathiasbynens.be/notes/reserved-keywords#ecmascript-...

Re: JavaScript fundamentals before learning React

#55
post #44
post #38

Earlier quoted context omitted.

isn't that functionally equivalent to `onClick={ this.smth }`?

no, because you're not passing the arg in now right?

Of course you are. The `smth` method will receive whatever arguments the caller gives.

This totally works but only if `smth` has been declared as an arrow function (so that is captures the class `this` context).

Re: JavaScript fundamentals before learning React

#56

This article has a lot to offer a JS + React beginner so it feels lame to reply to some tiny part of it, but this bit is a constant error propogated in the ecosystem: > Even though it is possible to mutate the inner properties of objects and arrays when using const, the variable declaration shows the intent of keeping the variable immutable though. let and const only control mutability of the reference. They say noth…

> let and const only control mutability of the reference. They say nothing of the value mutability. const then can only be a signal that you are not reassigning to the identifier.

Wouldn't this definition suggest that the value CAN be reassigned? The reference could be immutable but allow value reassignment, but is not the implementation of const.

Re: JavaScript fundamentals before learning React

#57
The biggest fundamental is that you only rarely need new (when forced upon you by a bad API) and you don't need this (not ever).

That statement has worked well for me in this language. I know it causes many people to cry and get immediately angry. The bottom line is that they increase the verbosity (substantially) of code, they are completely optional, and they often compound code maintenance through a more convoluted flow control.

Re: JavaScript fundamentals before learning React

#58

The biggest fundamental is that you only rarely need new (when forced upon you by a bad API) and you don't need this (not ever). That statement has worked well for me in this language. I know it causes many people to cry and get immediately angry. The bottom line is that they increase the verbosity (substantially) of code, they are completely optional, and they often compound code maintenance through a more convolute…

I can understand not liking `this` in JavaScript, but it seems optimistic to say you never need it. If nothing else, you are bound to encounter large amounts of code that uses it, so being familiar with its behaviour is necessary to understand that code.

Re: JavaScript fundamentals before learning React

#59
post #34

Earlier quoted context omitted.

It's been astonishing to me how people switched from var to let, but seem confused as to when to use const. I use const wherever I can since it's more appropriate for non-changing variables, and it helps me more easily visualize how a variable is being used in a block. But in other people's code I see a lot of letting all over the place. Maybe it's because of those articles saying to use let instead of var, but gloss…

maybe it’s because const is 2 more letters to type. if “let” was “letitbe” I bet const would be more popular seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.

How about balancing out every "let" with a "forbid" at the end of the block to mark the end of the scope?

Re: JavaScript fundamentals before learning React

#60
post #53
post #34

Earlier quoted context omitted.

maybe it’s because const is 2 more letters to type. if “let” was “letitbe” I bet const would be more popular seriously though, I wish they could have chosen a 3-letter word for const in keeping with var and let. I know const exists in other languages, but it doesn’t even mean the exact same thing as some other languages anyway.

> I wish they could have chosen a 3-letter word for const in keeping with var and let. "set" or "fix" would be quite nice, given what const actually does.

I'm having one of those days when variables won't and constants aren't.
Post reply on HN