3

I'm looking into some different techniques on inheritance in JavaScript. I have been using the NodeJS util.inherits function for my latest project. And I was wondering:

  • Why is the super_ property is set on the constructor instead of the prototype?
  • And wouldn't it make more sense to assign it the super constructor's prototype value instead? As the constructor can always be retrieved from the prototype constructor property?

so instead of this:

constructor.super_ = superConstructor;
    constructor.prototype = Object.create(superConstructor.prototype, {
        constructor: {
            value: constructor,
            enumerable: false
        }
    });

it would be this:

    constructor.prototype = Object.create(superConstructor.prototype, {
        constructor: {
            value: constructor,
            enumerable: false
        },
        super_:{
            value: superConstructor.prototype,
            enumerable: false
        }
    });

This way calling the super function is a lot easier, because instead of this:

 this.constructor.super_.prototype.someFunction.call(this);

it would become this:

this.super_.someFunction.call(this);

This makes more sense to me but I'm curious if I'm missing something. I also made a JSFiddle showing this implementation.

Thanks in advance for your reply!

SnailCrusher
  • 1,354
  • 12
  • 10
  • Why would you prefer `constructor.prototype.super.…` over `constructor.super.prototype.…`? – Bergi Jan 08 '15 at 23:29
  • Not sure whether this is a duplicate of [JavaScript inheritance and super constructor](http://stackoverflow.com/q/24699276/1048572) (which is about Coffeescript's `super`, not node), but the answer is the same. – Bergi Jan 08 '15 at 23:34
  • 1
    @Bergi Thanks for your reply, I see the problem now (: `this` would not be what you think it is when it is called as an inherited function. As for not saving the `prototype` instead, that makes sense as well, as the `prototype` could be replaced later on and the class would not reflect the changes if it would still refer to an old prototype object. I'd give you an up-vote but I'm afraid I don't have enough credits just yet :( – SnailCrusher Jan 09 '15 at 14:22
  • @Bergi with prototype you can do `this.super` for objects and `Class.prototype.super` for Class itself. – Ali Shakiba Feb 12 '15 at 09:07
  • @JohnS: Only that it apparently does not work with inheritance. – Bergi Feb 12 '15 at 11:08
  • @Bergi Ok, I read the answer you have linked. In Java compiler takes care of *this* and avoids this kind of issues. – Ali Shakiba Feb 12 '15 at 19:05
  • @JohnS: Only that in Java `this` means a completely different thing??? – Bergi Feb 12 '15 at 22:00
  • @Bergi If an instance variable (not method) is overridden in a subclass, `this.variable` in super class still refers to super class variable, not subclass. Moreover there is a `super` keywords which always refers to immediate super class. – Ali Shakiba Feb 12 '15 at 22:36
  • @JohnS: Yes, just what I say: a completely different thing. Btw, ES6 will have a `super` keyword as well – Bergi Feb 12 '15 at 22:42

0 Answers0