r/ProgrammerHumor • • Mar 20 '24

Meme areJSDevsActuallySane

Post image
14k Upvotes

554 comments sorted by

View all comments

Show parent comment

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.