Hacker News new | ask | show | jobs
by fulafel 3836 days ago
> JS is in exactly the same league with Python and Ruby and Perl other similar dynamic languages

There's a big difference to strongly typed dynamic languages that go out of their way to catch programming mistakes at runtime. With JS you get some "wtf" result when things go wrong and it propagates a long way before manifesting (if it's caught at all). Combine with JS's many weird semantics and special cases that are hard to keep in mind at once...

Recent favourite (minimized case after discovering unexpected piles of NaN's in some faraway place)

  > parseInt("0")
  0
  > parseInt("1")
  1
  > parseInt("2")
  2
  > ["0", "1", "2"].map(parseInt)
  [ 0, NaN, NaN ]
1 comments

Can you explain that one? Does it have something to do with parseInt being able to take more than parameter?
Array.map does not work like map in other languages.

They decided that map shall receive the array index as the second argument, and there's also a third argument that does something else. JS happily mashes these 3 arguments into parseInt's argument list of (stringValue, radix) without error. Hilarity ensues.

(This code also sins against "never call parseInt without the radix argument", see point about too many minefields to remember at once)

Seems to me the answer here would be to bind the radix argument of parseInt with a wrapper before applying map