(This isn't a specific proposal, I'm just seeking to have a general conversation about the direction we want to take Javascript, and wasn't sure the best place to post this)
In this post, I'm hoping to have an open-ended discussion about the role of inheritance in Javascript's future. There's a lot of smart people in this form with lots of experience, and I think we can get some good, meaningful ideas out of a discussion like this. I want to have this discussion, partially to help guide us in our decision making of the future of Javascript. I also realize there's going to be a lot of mixed opinions about this topic - this seems like an opportunity to hear each other's voices and learn from each other.
I'll start with a bit of background.
I know that for a good period of time, inheritance was all the rage - it got used left and right to solve all sorts of problems. Since then, there seems to be a backing-off, with a realization that inheritance also carries a number of problems, and maybe there are other tools available that could solve some of those problems in better ways.
I've seen a handful of proposals on these forms asking for features such as protected members, abstract classes, etc. These are features that have been around in many languages for a while, and are all heavily related to inheritance. I can't help but wonder if these types of features are still applicable today. I also see current proposals trying to build inheritance hierarchies on new built-ins for different reasons, and wonder if such hierarchies should really be done, or are we just trying to stay consistent with how we've always done things in Javascript. I've noticed that newer languages such as Go and Rust don't even provide inheritance, and wonder if there's something to be learned from them. I hear a lot of people on the internet shouting down inheritance, some saying it should rarely be used, others saying it should never be used.
I've been doing a lot of research into it on my own, trying to figure out the kinds of problems that inheritance is supposed to solve, and how those problems could be solved without inheritance (Some mainstream languages don't support some of the solutions out there, Javascript included). The more and more I look into it, the more and more I'm becoming convinced that, given a well-designed language, there really isn't any need for inheritance.
For example, often inheritance is used to customize the behavior of a particular class. Instead, the class can provide hooks into its behavior, which allow customization during construction. This is called the "strategy pattern", and it gives the class more power over what can and can not be customized, and generally results in more readable code on the end-user's side over an inheritance solution.
Another example: Often inheritance is used purely for the purposes of publicly exposing the fact that two different types can be used interchangeably (i.e. polymorphism). This can be done through the upcoming protocol proposal instead.
So, here are my discussion questions:
- What are some other use cases for inheritance?
- When should inheritance be used (if ever)?
- When should inheritance be avoided?
Answers to these questions can help guide us in knowing whether or not it's a good idea to provide inheritance-related features, such as abstract classes. It can also help us know when it's appropriate to make an inheritance hierarchy with new Javascript built-ins. I realize this probably won't result in exact answers, as there will be a diversity of opinions here, but we can still learn from each other.