4 ms·
> Looking forward to the inheritance/prototype section, something that's never quite stuck for me. I glanced through the section and I don't know how much it's
by baron816 9y ago
> Looking forward to the inheritance/prototype section, something that's never quite stuck for me.
I glanced through the section and I don't know how much it's going to help.
What helped me was to stop thinking that JS had inheritance at all. Inheritance implies some sort of copying and combining of object behavior. In a language with classical inheritance, a child class has all the methods of a parent class. A instance object of a class has all its methods plus all of its parent's methods directly on it.
JS doesn't do that. When you create an object that has a parent, you're creating two objects (the parent and the new child). When you call a method on the child that comes from the parent, the child doesn't hold that method, the parent does, so it delegates to the parent.
So, I think when people ask you in an interview whether JS has classical inheritance or prototypal inheritance, you should say neither--it has behavior delegation.
Please let me know if that makes sense.
- chris_wot 9y agoIsn't delegating to the parent object prototypical inheritance?
- baron816 9y agoYes, that's what we call prototypal inheritance. What I'm saying is that I don't think it's a good name for what's going on, since it clearly causes a lot of confusion.
- paulddraper 9y agoEh, I think that's debateable. Agreed that delegation is more clear though.