More information about the Underscore mailing list

[_] JavaScript inheritance

s'unya sunya.dickman at
Fri Sep 16 14:04:22 BST 2011

On 16 September 2011 11:34, Nick Morgan <skilldrick at> wrote:

> I think most JavaScript inheritance strategies are pretty rubbish. The
> modern 'classic' way is to use Crockford's Object.create, which is in
> ES5. That takes an instance and creates a new object that inherits
> from that instance (which is the way prototypal inheritance is *meant*
> to work). You don't need to go near a constructor function/use the new
> operator when you use Object.create.

I think it depends on what you are expecting; clearly it isn't a class based
language. Crockford's object pattern (with an extra reference to the parent
object) I like as a decent prototypal inheritance that doesn't have too long
a prototype chain and the returned object can neatly override original
properties. I probably use this pattern most of all.

> The problem with doing Child.prototype = new Parent(); is that you
> don't get any link to the parent constructor - instanceof won't work.
> I guess it depends how much you want to pretend that JS uses
> class-based inheritance. I generally prefer to use different reuse
> techniques that don't rely on inheritance.

So not inheriting the constructor can be useful too; some thing like having
a private methods in a super class. My trouble with it is that you get a
very long prototype look up chain which can't be efficient. Tim, I assume
that this is what you are talking about by Prototype2.prototype = new

or do you inherit the parents' prototype to reduce the lookups? In which
case how do you get over them all sharing the same prototype - ie. child
changes parents' prototype?

I guess it's what you're most comfortable with, but there must be some
patterns that just stand out as being better than others. JS is such a
versatile language, that it seems that there are almost unlimited ways to
make inheritance!

While we're on the subject, has anyone used web-workers in earnest? I'm
considering a JS application that degrades from a 'multi-threaded', web
worker application to a single threaded version and wondered what the
general experience was.


            o                 o
                O   O   O
      o  o   O s´unya O   o  o