r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

Show parent comment

16

u/Some-Guy-Online Mar 20 '24

RegExp is a built-in, so it is in fact JavaScript doing it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

And yes, the argument is that library functions should validate input types.

I would understand if there is some explanation for why this happens, but it's not readily apparent.

1

u/elehisie Mar 20 '24

This kind of thing is the reason why Typescript exists. I do agree checking the type of the argument would most likely be way more beneficial than not doing so. Seems like whoever wrote RegExp expects ppl will read the docs and use it “properly”. Can’t really say that’s a reasonable expectation 🤷‍♀️

1

u/AaTube Mar 21 '24

because javascript convention is anarchy, no input type checking anywhere at all for you!

(of course, there are checks like, e.g., date constructors, but as a very amateur person i haven't encountered shenanigans yet)

1

u/_xiphiaz Mar 21 '24

The explanation is fairly straight forward - in apis where a string is expected, but a non-string is passed, JS will attempt to call .toString() on that method. Vanilla objects are the base case and return [object Object] when .toString() is called.

It would be a rather weird use case fo RegExp to need to recieve some object that intentionally implements toString in order to produce a valid regexp but that would be an inconsistency with standard expectations that .toString is the standard fallback.

This exists in other numeric apis too, where valueOf is attempted on objects.

1

u/Some-Guy-Online Mar 21 '24

You're just repeating the same useless stuff a ton of other people have already said.