0

I have written stuff in other languages where you inherit a class, and the super class constructor is called when and object is created of the subclass. However, in the pattern I find all over for inheritance from javascript, the super constructor is actually run when inheritance is established.

Example:

var thingy = function(){ this.constructionTime = new Date() };
var thing2 = function(){ this.type="thing2" };
thing2.prototype = new thingy();//thingy constructor actually runs here
var t2 = new thing2();//does NOT call thingy constructor
setTimeout(function(){
  var t3 = new thing2();
  t3.constructionTime == t2.constructionTime; // TRUE
},100);

Then I found a few, much less common, examples where they did something like this:

var athing = function(){
    this.constructionDate = new Date();
}
athing.prototype.showDate = function(){ console.log(this.constructionDate) };

var something = function(){
    athing.apply(this);
    this.note = "I'm something";
}
var x = new something();

then calling x = new something() does run the constructor, but does not inherit methods. So I add

something.prototype = athing.prototype;

which does not give x the methods, but new objects

y = new something();
y.showDate();//shows the date generated during its construction

do have them.

So here's my perhaps overly broad question(s): am I missing something? Is there a reason NOT to use this pattern other than wanting your super constructor to only be run once?

Aerik
  • 2,307
  • 1
  • 27
  • 39
  • It isn't always desirable to call the super constructor. – Oriol Nov 20 '14 at 22:01
  • Okay, I've seen code where the prototype doesn't initialize any variables, etc., but only provides some methods, so in that case running the super constructor is unnecessary, right? But taking my code as an example, where the constructor not only does something, but it does more than initialize variables to fixed values... – Aerik Nov 20 '14 at 22:15
  • Inheritance by setting Child prototype to Parent prototype is not good. A dog is an Animal but an Animal isn't always a Dog. As in the given answer; it's better to use Object.create. The following answer may e helpful to you: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Nov 20 '14 at 23:30

2 Answers2

0

When you want subclassing, consider using

function Constructor() {
    SuperConstructor.apply(this);
    /* ... */
}
Constructor.prototype = Object.create(SuperConstructor.prototype);
Object.defineProperty(Constructor.prototype, 'constructor', {
    value: Constructor,
    configurable: true,
    writable: true
});
  • Calling SuperConstructor inside Constructor makes Constructor instances have properties set inside SuperConstructor.
  • Setting Constructor.prototype to Object.create(SuperConstructor.prototype) makes Constructor instances inherit properties from SuperConstructor.prototype.
  • Since Constructor.prototype has been replaced, constructor property must be added manually. Use defineProperty in order to make it non-enumerable.
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Okay, that looks cool, but why is it better than the code in my question? The first part - SuperConstructor.apply - is the same thing as in my question. I don't quite understand the next bits... – Aerik Nov 20 '14 at 22:27
  • @Aerik Yes, the first part is the same. But your `something` instances don't inherit from `athing.prototype`. – Oriol Nov 20 '14 at 22:33
  • What do you mean specifically by "don't inherit from"? When I set the "something.prototype = athing.prototype;", doesn't that create inheritance? My y object has the showDate method... (Not trying to argue, just trying to understand the details of what you're saying) – Aerik Nov 20 '14 at 22:38
  • @Aerik Ah, sorry, I missed that line. Then yes, they will inherit, but that isn't a good way of inheritance, because this way you can't add properties to `something` but not to `athing` ones. – Oriol Nov 20 '14 at 22:45
-1

Javascript is a class free language. It's not a good idea to try and shoehorn classical concepts like inheritance and super constructors into it.

  • It may be class free, but "not a good idea to try... inheritance"? See http://www.crockford.com/javascript/inheritance.html – Aerik Nov 20 '14 at 23:12
  • Sorry I didn't mean don't ever use inheritance, I meant specifically inheritance with the super constructor concept. Prototypal inheritance should absolutely be used! – Will Hitchcock Nov 20 '14 at 23:21