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!