4 ms·
This is a great example of why I wish all function parameters except a first non-optional one always had to be named. In other words, you could do: parseInt
by crazygringo 3y ago
This is a great example of why I wish all function parameters except a first non-optional one always had to be named.
In other words, you could do:
parseInt("1");
And you could do:
parseInt("1", radix: 16);
But you could never do:
parseInt("1", 16);
I swear, this is my #1 wish for programming languages in general. Identifying parameters by position was necessary back when memory was at a premium, but it's always been a disaster for readability and clarity. And we're still all paying the price.
Edit: And I'm not talking about named arguments as an option (lots of languages provide this, though not JavaScript). I mean required, where the very concept of argument order (except for first) doesn't even exist.
- jvanderbot 3y agoThat's not the problem. The problem is when a( <a list L> ) becomes a( l[0], l[1], l[2] ), which is bananas. you pass one object, you should stuff that into the first parameter.
- crazygringo 3y agoIt would prevent the bug, though. Passing the index and object for .map() can come in handy sometimes, so I understand the motivation for including it. But with named arguments, they could be passed as something like "_index:" and "_obj:", rather than getting slotted in as an arbitrary [1] and [2], where they can be misinterpreted as other parameters (like "radix").
- deleted 3y ago[deleted]
- deleted 3y ago[deleted]
- matt_kantor 3y agoI might be misunderstanding you, but if you're saying this is what's happening with `["1","2","3"].map(parseInt)`, that's not correct. The article describes the situation in detail, but `parseInt` is called three separate times (its first argument in each call is `"1"`, then `"2"`, then `"3"`, as you'd expect with any array-mapping operation). The weirdness arises because `.map` always passes more than one argument to the callback (and `parseInt` accepts more than one parameter). This happens even when the array has only a single element, it's just that the first call (`parseInt("1", 0, ["1"])`) happens to have the same return value as `parseInt("1")`, so you only notice the weirdness with the later elements.
- jvanderbot 3y agoI see! Thank you for clarifying.
- deleted 3y ago[deleted]
- cute_boi 3y agoif you use visual studio code, i think there is a feature that will give the feature you intend.
- kerblang 3y agoI think we're off on a tangent here, but I will at least agree that named parameters are a godsend in any programming language. Things are so much easier to read when the caller can clearly state their intentions for things like "foo(true, false, true)".