News for hackers. Stuff that matters.
Re:Stranger Things, JavaScript Edition by jdlshore on Thursday June 04, 2020 @01:04AM (#23412650)
"If I want to be taken seriously?" Was that bit of petty oneupmanship really necessary? I assure you, Alex, I'm completely unconcerned with whether you take me seriously.

You were probably just trying to show off. But in case you weren't, here's how JS works.

In JavaScript, if you're passing in a function as a parameter, the normal way is to use an arrow function. Like this: `array.map((element) => yourFunction(element));`. This gives you explicit control over which parameters are passed in to your function and how.

As a shortcut, you can provide a function name rather than an arrow function. If you do so, you're saying that you want all parameters to be passed to that function. You're expected to know what those parameters are and how the function you're passing in will use them. If you don't know that, well, you're programming blind and bad things will sometimes happen. Maybe don't do that.

As with many things in JavaScript, it's better to be explicit than rely on the shortcut.

Edit: Just to be clear, here's the correct way to map an array of strings to an array of numbers in JavaScript: `array.map((s) => parseInt(s, 10));`

Re:Stranger Things, JavaScript Edition by tomcatfish on Saturday June 06, 2020 @01:06AM (#23436967)
It wasn't meant to be oneupmanship, just that you were deliberately ignoring the core of the argument. I could make an article about why NULL in C is a wonderful feature and how it allows such nice optimizations, or I could acknowledge that it has a great cost too. You basically said "There's no downside if you are as smart and experienced as me", but with respect to Javascript, much of the world (myself included) are not. Your argument came down to "people with sufficient skill don't make this mistake", which was definitional instead of actually addressing that the default behavior of an actual usecase was not sane.
Re:Stranger Things, JavaScript Edition by BrandoElFollito on Thursday June 04, 2020 @05:27PM (#23422040)
I am an amateur dev and used the arrow notation only in the way you described in your first example: array.map((element) => yourFunction(element))

What exactly is the shortcut you mention? (wild your mind converting the example into the incorrect/dangerous version?)