Robin-Hoodie
Robin-Hoodie

Reputation: 4974

Prototypical inheritance without creating a parent object

So I was reading the book Head First Javascript Programming, which is a great book btw, learned me a lot, when I stumbled into the subject of multiple inheritance.

The book taught me that to achieve multiple inheritance you should set the prototype of your constructor to a new object of your parent.

As an example:

    //Parent
    function Dog (name){
       this.name = name; 
    };



   //Child
   function AwesomeDog(name){
      Dog.call(this, name);
   };
   AwesomeDog.prototype = new Dog();
   AwesomeDog.prototype.constructor = AwesomeDog;
   AwesomeDog.prototype.isAwesome = true;

Now I don't agree that this is the best way to do it, you call the Dog constructor only for its prototype property, but you also get the name property with it, that you will typically never use and therefore always be undefined.

Why not just do this:

 AwesomeDog.prototype = Dog.prototype;

You're only interested in the prototype anyway so why not?

In my opinion this is better because of 2 reasons:

  1. The overhead of creating a new object is left out (insignificant, but it's something)
  2. You don't have useless properties floating around in your child's prototype, that will probably always remain undefined.

Now this book has been brilliant in every way so that led me to believe that I perhaps missed something crucial, but I can not see what. I can not imagine any use cases where having double properties (one in the child object and the undefined version in your prototype) can be useful.

Is my way of thinking correct and can I stick with my method?

Upvotes: 1

Views: 104

Answers (2)

Oriol
Oriol

Reputation: 288000

Now I don't agree that this is the best way to do it, you call the Dog constructor only for its prototype property, but you also get the name property with it, that you will typically never use and therefore always be undefined.

You are completely right. Using new has these problems, and thus is discouraged.

Why not just do this:

AwesomeDog.prototype = Dog.prototype;

This has a problem: both AwesomeDog and Dog instances share the same prototype. Therefore, they will inherit the same properties and methods, so there is no point in subclassing. And which should the constructor be, AwesomeDog or Dog?

Instead, the proper way is assigning AwesomeDog.prototype to an object which inherits from Dog.prototype, but without calling Dog. You can do this with Object.create:

AwesomeDog.prototype = Object.create(Dog.prototype);
AwesomeDog.prototype.constructor = AwesomeDog;

Upvotes: 0

Denis Priebe
Denis Priebe

Reputation: 2855

Object inheritance in JavaScript is also the basis of constructor inheritance. Every function has a prototype property that can be modified or replaced. That prototype property is automatically assigned to be a new generic object that inherits from Object.prototype and has a single own property called constructor. In effect, the JavaScript engine does the following for you:

// you write this
function YourConstructor() {
    // initialization
}

// JavaScript engine does this for you behind the scenes
YourConstructor.prototype = Object.create(Object.prototype, {
    constructor: {
        configurable: true,
        enumerable: true,
        value: YourConstructor
        writable: true
    }
});

So without doing anything extra, this code sets the constructor’s prototype property to an object that inherits from Object.prototype, which means any instances of YourConstructor also inherit from Object .prototype. YourConstructor is a subtype of Object, and Object is a supertype of YourConstructor.

Upvotes: 1

Related Questions