2 ms·
When you add a new optional parameter to a function, there is no guarantee that you won't break code that calls the function. Here is one notorious example:
by Stratoscope 6y ago
When you add a new optional parameter to a function, there is no guarantee that you won't break code that calls the function.
Here is one notorious example:
[ '1', '1', '1', '1' ].map( s => parseInt(s) )
This returns the expected result:
[ 1, 1, 1, 1 ]
Then you think, "I don't need that little wrapper, I can just pass parseInt as the callback directly":
[ '1', '1', '1', '1' ].map( parseInt )
And this returns something that you may not expect:
[ 1, NaN, 1, 1 ]
That happens because parseInt() takes an optional second parameter with the number base, and map() passes a second parameter with the array index. Oops!
A similar situation can arise when you add a new optional parameter. You still have to check all the call sites, and for a public API you won't be able to do that.
- jchw 6y agoThis is true, I mainly didn’t bother mentioning it since it is still a fairly obscure edge case (relative to just getting started in JS anyways,) and because TypeScript catches that too, most of the time.