Deep JavaScript Foundations, v3

Inheritance is Delegation

Kyle Simpson

Kyle Simpson

You Don't Know JS
Deep JavaScript Foundations, v3

Check out a free preview of the full Deep JavaScript Foundations, v3 course

The "Inheritance is Delegation" Lesson is part of the full, Deep JavaScript Foundations, v3 course featured in this preview video. Here's what you'd learn in this lesson:

Kyle comes to a conclusion about the design of JavaScript's inheritance system and elaborates on its strengths.


Transcript from the "Inheritance is Delegation" Lesson

>> Kyle Simpson: By the way, this prototype system, not bad at all. It's incredibly amazingly unicorn design, it's very powerful. It's just a different design pattern than classes. It's a different design pattern which is called delegation. Now, I didn't make up this term, by the way. I've been studying the prototype system and how these things work for a long time.

I didn't make up delegation, what I did was I understood, I broke down all the pieces of how JavaScript works and the linkages and it's not copy. And I built up this mental model of the system and then one day, I was just surfing on the internet trying to research stuff and I ran across a Wikipedia page called Delegation, and I started reading about this design pattern called Delegation.

And I was like, that sounds really, really, really familiar. The light bulb moment, that's what JavaScript is. I didn't invent the term. Delegation or behavior delegation. I just called an apple an apple. That's what JavaScript's system is. Its prototype system is a delegation system, not a class system.

If you think about delegation and classes as different design pattern. Rather than saying one is bad and the other is good, I'm not saying that classes are bad. I'm just saying they don't fit in a system that was designed to be a delegation system.
>> Kyle Simpson: And we've been trying to make it fit and we keep trying to shove it in there and duct tape it to make it fit.

And maybe the more effective thing is to say, hey, system, how are you designed? What amazing things can I do with you if I use the way that you actually work? This thought is not unique to me. But it has been observed that, if you're comparing a prototypal system, a delegation system, to a class system, that the prototypal system is actually significantly more powerful than a class system.

And here's why, it is as if the prototypal system is sort of the super set and the class system is a subset. And here's why, because you can implement a class system inside of a prototypal language, but you cannot do the reverse. You cannot implement a prototype system in a class-oriented language.

So we chose to take this amazingly powerful system and use it in one very specific way that's not even a particularly good usage of the system. And then we spent 20 years banging our heads. Why doesn't this work like it does in other languages? My response is because you're doing the wrong pattern.

You can do classes. And some of you have to do classes because you work with frameworks that make that choice for you. But what I don't want you to do is walk away from this discussion thinking well, it's my only option. It's not your only option. This system exists, and there's so much untapped potential.

And what I wanna dig into in our final time together in this unit, what I wanna dig into is, what can we do with delegation if we set aside our preconceptions that classes are the only design pattern that matters?

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now