I'm not saying these issues make the language unusable, but you've got to be a bit more honest that you were if you want to be taken seriously.
I'm not saying these issues make the language unusable, but you've got to be a bit more honest that you were if you want to be taken 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));`
What exactly is the shortcut you mention? (wild your mind converting the example into the incorrect/dangerous version?)
I use map() all the time but I didn't know about that until I read this article. I wonder why map() functions this way in Javascript and if any other languages pass additional values like this in their own map() implementations.
I do think GP has a fair point of criticism - at least when I use map() I expect it to take each value in the array and pass it to the callback function. I don't expect it to take each value in the array, the index of that value and the entire array and pass all of that to the callback function instead.
Sometimes it's very convenient to be able to look ahead or behind the current element being map()'d to make decisions.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Then you don't know the basics of the language you're using- how can you complain about it?
> when I use map() I expect it to take each value in the array and pass it to the callback function. I don't expect...
map is well documented. Making the element's index available is indeed pretty useful (the entire array less so, but sometimes it can be).
As an example of one of the many ways to solve this to make it more sane, the Rust language allows you to call .iter() on something that can be looped over, but also .enum(), which gives you an index and the item. In this way, you can manage expectations about what you are actually getting. Javascript hands you all the tools at once unconditionally which strips a lot of the abstracting power away.