r/programminghorror
•
u/Leonnee
•
Jun 12 '25
•
1
Javascript Javascript is filled with horror
2.4k
Upvotes
View all comments
Show parent comment
1
u/LordFokas Jun 18 '25
It's hard to make a bridge there when the types systems are wildly different. I think it's more important to consider if you can achieve the same or similar functionality, let's say, be functionally equivalent, rather than asking if it is formally equal. Do you see where I'm going?
We agree on duck typing. That's one small step.
I want to break overloading down a bit:
1 - optional parameters with defaults. May or may not count as overloads in your book. I'm fine with both. This is simple and easy, looks nice, zero trouble.
2 - different types for the same argument: it's typeof and instanceof all the way down, or alternatively duck typing checks. Looks a bit weird, for very extensive overloading it may certainly begin to get unwieldy. In general I'm not very concerned with the troubles of this approach because it's not a good practice even in languages with real overloading so I don't abuse it, but the weirdness is there, yes.
3 - adding an overload to someone else's method: No. You need to replace the entire method in one go. However you can save the old method in a variable (functions are first class citizens -- oh right, Function is also a type, I knew I missed one) and just call it later instead of overriding it.
I think the biggest barrier for you here are the formalities that come with types, which as a Java person, I totally understand, believe me. JS is not a very formal language, and so the problem solving process needs to be a bit more lax.