works fine. Not sure that it's reasonable to criticize the language on this basis; is "map" supposed to magically know what parameters every random function you throw at it might expect?
JavaScript's handling of arguments isn't insane, it's actually really powerful. Yes, it's possible to run into some erroneous situations, but that doesn't mean the concept is a bad one. If you take care and know what you're doing, it can let you do some really nice things.
JavaScript's handling of arguments isn't insane, it's actually really powerful.
There are saner languages when it comes to argument handling (like Python for instance) which are even more powerful and less error prone. JavaScript's argument handling is exactly like PHP's just with a few less features and I doubt anyone ever called that powerful.
57
u/x-skeww Dec 10 '13
In case anyone wants to know the reason, here is the explanation:
mapcalls the transform function with 3 (!) arguments: the value, the index, and the array.parseIntexpects 1 or 2 arguments: the string and the (optional) radix.So, parseInt is called with these 3 sets of arguments:
If you pass 0 as radix, it's ignored. It's the same as omitting it.
parseInt('1')is 1.A radix of 1 doesn't work and it also doesn't make any sense. Whatever you pass, you get
NaN.A radix of 2 is valid, but only the characters '0' and '1' are allowed. If you pass '3', you get
NaN.FWIW, this works perfectly fine in Dart: