Hacker News new | ask | show | jobs
by wruza 617 days ago
Thanks, this added to my standard low-level experiments in a new language. The most interesting (or should I say well thought-through and at the same time tricky) part of js is how prototypes and properties work. Rarely a language has a similar complexity at that level, but I started to respect it the second I’ve got an overview, cause it addresses well the pain points that simpler designs usually have.

That said, for me it’s hard to buy into his arguments, in a sense that it doesn’t matter that much, if at all. instanceof doesn’t work for different realms and is nuanced for direct prototyping and Object.create(), but I never use or care about these in my code, by design. There’s no way that such value could appear in a false-negative instanceof comparison, so. A similar thing happens in COM/OLE integrated runtimes, where you have to be careful with what quacks like a date or a string but is neither due to a wrapper. But that’s expected.

I believe the real issue here is that iframes/etc usually get served as “some values aren’t, so use X, be careful” rather than “guys it’s an effing wrapper to an effing different runtime, which we found to be an overall anti-pattern many years ago”. Browsers and webguys normalized it, well, they normalized many crazy stuff. Not my problem. There’s no need to learn to balance on two chairs when it’s not what you do when sober. I still use Array.isArray(), but only because every linter out there annoys you to hell into it.

Tldr: classes are neat you can pry them from my cold dead hands.

The only thing to care about with classes is to not fall into the inheritance trap, and not for the reasons of instanceof. Inheritance is a tree of ladders attached with a duct tape, you have to know what you’re trying to do to your design before thinking about it. Most sane use of inheritance is one-off from a library to a user (two separate developing agents agree on an implied behavior, “I implemented it for you to randomly extend and pass back” mode), or for helping type inference. Otherwise, a way to go is to eject a common behavior into a separate class or a couple of functions (aka composition).

1 comments

Fair points. I do use classes exposed by libraries. I don't like it but it beats the alternative.

I really like the flexibility of factory functions, that is the main point of the article imo.

COM/OLE ... that takes me back to the early 90s, a place I hoped to never visit again!

:)