Skip to content Skip to sidebar Skip to footer

Why Do We Use `Boy.prototype = New Human;` To Simulate Inheritance?

i don't get why everyone is using Boy.prototype = new Human; to simulate inheritance. Look, what we want is the function's of A right? we can do that without instantiating a new A

Solution 1:

A better option is to create an intermediate to hold the prototype.

function extend(clazz, superclass) {
    var intermediate = function() {};
    intermediate.prototype = superclass.prototype;
    clazz.prototype = new intermediate();
    // Following line is optional, but useful
    clazz.prototype.constructor = clazz;
}

This avoids unnecessary copying, but still means that you don't need to instantiate an object that will do work in its constructor. It also sets up the prototype chain so that you can use instanceof. It also doesn't result in superclass prototype contamination which some inheritance antipatterns can.

For completeness, your subclass should call the superclass constructor in its constructor, which you can do with Superclass.call(this);.

EDIT: Since ES5, you can replace calls to extend with

Subclass.prototype = Object.create(Superclass.prototype);

which does the same thing.


Solution 2:

  • It sets up the prototype chain correctly, meaning that instanceof and isPrototypeOf() will work
  • It's less verbose

There are simple but ugly workarounds to prevent a constructor function from performing its usual initialization when you're just using it to create a prototype object. For example, you could either check the arguments:

function Boy(name) {
    if (arguments.length == 1) {
        this.name = name;
        // Do other initialization
    }
}

... or move the initialization into a separate method that has to be called explicitly:

function Boy(name) {}

Boy.prototype.init = function(name) {
    this.name = name;
    // Do other initialization
}

This has the obvious downside of requiring you to remember to call init() whenever you create a new Boy.

In ECMAScript 5 environments (current versions of most browsers, for example), a better option may be ECMAScript 5's Object.create(), which allows you to create an object which inherits directly from another object and sets up the prototype chain for you. This can be emulated (but only approximately: see Object.defineProperty in ES5?) in non-ES5 environments:

if (!Object.create) {
    Object.create = function(o) {
        function F() {}
        F.prototype = o;
        return new F();
    }; 
}

Post a Comment for "Why Do We Use `Boy.prototype = New Human;` To Simulate Inheritance?"