2 ms·
I was taught that the practical purpose of inheritance is to share code. Nowadays we call that idea "DRY". Classical inheritance is a means to satisfy DRY. In
by tantalor 14y ago
I was taught that the practical purpose of inheritance is to share code. Nowadays we call that idea "DRY". Classical inheritance is a means to satisfy DRY.
In JavaScript, the same concept exists, and it's sometimes called "prototypal inheritance" (since there are no classes).
http://javascript.crockford.com/prototypal.html http://javascript.crockford.com/prototypal.html
- jefffoster 14y agoI've always thought inheritance is used to model is-a relationships. Sharing code may be a side effect, but it's certainly not the reason to use it. Using inheritance to share code is usually better achieved by factoring that code out to a separate object and having the objects in a inheritance relationship use it by composition.
- justincormack 14y agoHmm, interesting view. But why would you want to model is-a relationships? Other than to share code?
- AndrewDucker 14y agoThink of it as enforced documentation. If I say that my object is a Customer then I know what can be done with it. If I have a method that says it takes a Customer object then the calling point knows what to pass in, and my method knows it can rely on that object satisfying certain conditions. Basically it removes (some of) the need to document separately, and it enforces that the documentation is adhered to.