Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It's interesting that so much time and effort is spent by JavaScript developers implementing classical inheritence when this is a concept increasingly shunned in classically typed languages.


Languages like C++, Java, C#, Objective-C, Python and Ruby make inheritance dead simple to use. There's one main way of doing it, it's very natural, and so it can often be used excessively.

JavaScript makes inheritance a hassle. There are numerous ways of faking it, and they aren't always compatible with one another.

I don't think we're seeing inheritance "shunned" in those so-called "classical OO" languages. Given more experience with the potential pitfalls of overuse of inheritance, I do think we're seeing it used less extensively than it once was, but it's still a useful technique.

On the other hand, we do see JavaScript developers putting a lot of time and effort into trying to add functionality that is inherently useful, but that isn't offered well by default. It's the opposite of the problem that those other languages suffered from. They make it too easy by default; JavaScript makes it too difficult.


Or maybe not so much too difficult, as pathologically convoluted.


I think it's almost like a challenge people do for its own sake because it's something even Crockford failed to do correctly. Write an awesome classical inheritance pattern, write a bunch of tests to convince yourself it works, and then ignore it.

Indeed, the fact Crockford's stuff on classical inheritance in The Good Bits was wrong but the book got (and retains) so much traction indicates just how much no-one cares.


Could you link to a good explanation of how that stuff fails please? Not doubting you, just want to read more.


It's been a while since I used it, and I can't remember what the problems were off the top of my head but they were face palm level.

Here's a discussion on stack exchange:

http://programmers.stackexchange.com/questions/173176/javasc...

The general problem with all classical inheritance patterns in Javascript is that they don't really work (they treat Javascript as a static language so you get all kinds of nasty surprises because it isn't). This is particularly sad in comparison with older, conceptually simple languages like Objective-C that do this stuff properly.

Crockford himself says it was a mistake to even include the section in the book (in 8 years he's never used it)

http://www.crockford.com/javascript/inheritance.html

The takeaway point is that doing classical inheritance in Javascript is a Bad Idea. If you think you've done it, you probably haven't. And no-one will use it.


The Good Bits had way more stuff than "how to write classical inheritance". Indeed, IIRC, the section right after that was "how to write prototypical inheritance".


Of course. There's lots of great stuff in Good Bits but the fact it had egregious errors in its section on classical inheritance and no-one really noticed says a good deal about how much it matters for Javascript programmers.


>when this is a concept increasingly shunned in classically typed languages.

It's only shunned by a certain class of hipster and more-functional-than-thou programmers. The vast majority of classically typed language programmers still use classical inheritance.


No, there's just not that many true instances of the is_a relationship outside of shapes. Reuse is often better achieved via composition rather than complex up-and-then-down-again virtual call chains.


>No, there's just not that many true instances of the is_a relationship outside of shapes.

I was talking about if the majority shuns inheritance or not [it does not]. Not whether it's the correct way to go.

That said, there's lots of instances of is_a relationship. Anywhere where you have different types of something.

A work hierarchy for example. While composition might work for the methods we want to have, conceptually a manager is not someone who HAS an employee (composition), he IS-AN empoyee.

GUI widgets. Game sprites. Vehicles. Animals. Simulation objects.

Consider how the "semantic web" is all about taxonomies -- put everything into taxonomies.


It's shunned in most languages because it's often the only way to do things to offer. OOP is really appropriate in some circumstances but it's not the ultimate paradigm that solves all your problems. Javascript offers some facilities that makes it more polyvalent.

Disclaimer : I'm not actually a big fan of javascript.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: