A friend of mine encountered this page by Michael Bolin, author of Closure: The Definitive Guide. It’s a very great writeup that distills a lot of JavaScript edge-cases and “gotchas” for a person just starting to get advanced in the language. I would have appreciated something like that back in the day and like most just learned it slowly through experience and bug-bashing over the years.

Though the page did cover prototypal inheritance well and it’s powerful ability to do pretty much everything that regular inheritance models of languages like Java can do, one small bit that the page didn’t cover was trying to call super on a parent class. The issue is that there is no built-in “init” method on a JavaScript constructor. The simple act of initializing a “class” in JavaScript both creates the object and calls its setup routine inside the class. This might be perfectly fine in most people’s day-to-day Javascript but if you ever need your parents class to do some additional setup work for example in the case of a factory pattern, you need this support. JavaScript frameworks like Backbone.js and jQuery invented their own ways to solve this issue but it’s important to know the minimum amount of effort to solve this problem without relying on a 3rd party framework.

The answer is to create an empty “surrogate” class to hold the parents prototype. That way when it is initialized, you have the chance to run the contents of constructor when you’re good and ready:

function Mammal(obj) {
    this.name = obj.name || 'Anonymous Mammal';

    // Basic conditional code you normally couldn't do.
        this.category = 'marsupial';

Mammal.prototype.legs = 4;

var lion = new Mammal({ name: 'Lion' });
var tiger = new Mammal({ name: 'Tiger' });

// This middle constructor is required because we can't instantiate `Mammal` directly as `Marsupial`'s prototype
// or else it would run through its "initialize" stuff too early.
function Surrogate(){};
Surrogate.prototype = Mammal.prototype;

function Marsupial(obj) {
    Mammal.apply(this, arguments);

Marsupial.prototype = new Surrogate();
Marsupial.prototype.pouch = true;
Marsupial.prototype.legs = 2;

var kangaroo = new Marsupial({name: 'kangy'});

The above example is highly contrived but you can see that you can actually push off a bunch of responsibility from the child class to its parent. Which means less setup work for each child and the ability for the parent class to decide the “DNA” of the child based on the parameters passed in. For example if this was a vehicle parent class, mixin the diesel engine class or if the child is a truck or hybrid engine if the child is an electric car.