obj[dynamic_property] = /* … */;
… for which you must, rather unfortunately, be aware of prototypes.(And if you can use Map, you probably should… it avoids the JS footgun here.)
In mid-late-2000s when the first idea of adding classes to the syntax came up, even Microsoft eventually said: nah, it's a bad idea. It's grafting incompatible concepts onto a language that doesn't need them.
And then Google in their "wisdom" went and forced classes onto the language. Take a guess why you need this for methods to work the way you expect them:
class Logger {
constructor () {
this.method = this.method.bind(this);
}
method (some, params) {
this.do_stuff()
}
/// ...
}`this`, of course, is a whole other bag of worms :(
You do indeed need to understand prototypes to explain this behavior. "Free functions" doesn't really make sense, because it obviates the need for classes.
This is not Google's fault. It is the fault of Java developers. They expect everything to work like Java. They expect to find Java APIs everywhere.
I have never written classes like that. In fact, if I were to see a method being used in the way you are insinuating I would believe to have found a bug.
I strongly dislike class-based object orientation in general. But I'm glad we do not have to deal with dozens of ways to do OOP in JavaScript anymore.
It quite literally is. In there desire to push web components they also pushed quite a few (often questionable) solutions. See 17:59 here: https://youtu.be/y-8Lmg5Gobw
> But I'm glad we do not have to deal with dozens of ways to do OOP in JavaScript anymore.
There never were "dozens of ways". There always was only one: prototype based. And now there's this weird contraption on top that pretends its classes from C++-like languages (C++, Java, C# etc.) but is not.
class Logger {
method = (some, params) => {
this.do_stuff()
}
/// ...
}
But your overall point stands.It is, because classes are a thin wrapper over prototypes, but the whole "classic C++-like" class shenanigans translate very poorly to Javascript. And yet, with maddening bravery browsers have spent 10 years trying to make them work in some capacity (and don't get me started on the whole private members thing, or the autobind proposals, or...)
its pitfalls should be understood and avoided instead of being afraid of JS prototypes
I feel like the people who are in that situation know who they are. The rest of the world benefits from knowing what subset of the ecosystem they want to work with, as opposed to "you should know everything", which is the most inclusive and the least useful statement.