4 ms·
He calls inheritance in Javascript a wart. It's one of its major strengths, IMO: the fact that you can directly override methods for a single object. Class inhe
by bartl 15y ago
He calls inheritance in Javascript a wart. It's one of its major strengths, IMO: the fact that you can directly override methods for a single object. Class inheritance, as he'd like to see, is not all it's cracked up to be. Just look at Java with its anonymous inner classes, for one incredibly ugly construct...
BTW an easy way to produce traditional class inheritance in Javascript is to have a single instance from the class you're trying to inherit from, as the prototype. (To work properly that object should be "abstract": have no ties to any physical resources, like a browser widget, a database handle or a file handle.)
- masklinn 15y ago> Class inheritance, as he'd like to see, is not all it's cracked up to be. Just look at Java with its anonymous inner classes, for one incredibly ugly construct... I'm not sure why you're implying java's anonymous inner classes have much relation with class inheritance. The vast majority of class-based languages do not have (or need) anonymous inner classes (though some languages allow for creating unnamed classes on the fly by instantiating metaclasses).
- bgrohman 15y agoYeah, javascript's prototypal inheritance is flexible enough to simulate classical inheritance. I wouldn't call that a wart. http://www.prototypejs.org/learn/class-inheritance http://www.prototypejs.org/learn/class-inheritance http://dojotoolkit.org/reference-guide/dojo/declare.html http://dojotoolkit.org/reference-guide/dojo/declare.html Plus, I tend to prefer prototypal inheritance to classical inheritance. I do agree that the constructor pattern using the new keyword and the prototype object is a bit odd, but that's not the only way to have objects inherit from other objects in javascript.
- WayneDB 15y agoThe wart is that since classical inheritance isn't built into the language, we get: 1) Crummy syntax. 2) Everybody doing it a different way.
- bgrohman 15y agoI see your point to some extent. I don't think the functions for mimicking classes provided by libraries like Prototype and Dojo have particularly crummy syntax. But since there isn't a standardized way to create classes, implementations are usually a little different. Still, according to this logic, any language that doesn't support classical inheritance has warts. I'm not sure I agree with that. JavaScript doesn't have classical inheritance built in, but I haven't seen anything to make me think that it's really needed from a programming standpoint. Your point #2 is valid, but could that can be attributed to the programmers who use the language rather than the language itself?
- true_religion 15y agoI've used both methods of inheritance. Prototypical inheritance in the IO Language is a beautiful thing. Not so much in Javascript. Class based inheritance in Smalltalk and its dynamic mind-children Python and Ruby are similarly elegant and flexible. Not so much in C++ or Java. The problem isn't with Prototypical inheritance, its with the implementation of it in Javascript.