super constructor calls for max/min classes

Luke Hoban lukeh at
Sat Jul 7 20:14:47 PDT 2012

Two questions on maximally minimal classes.

1) Should a super constructor call be made if no derived constructor is specified?

The wiki and spec drafts suggest that no super call is made automatically on behalf of the developer if they leave off a constructor declaration in a derived class.  So the example below would run without error and print 'undefined'.  

    class Animal { constructor(name) { = name; } }
    class Snake extends Animal { }
    var sam = new Snake("Sammy the Python")

This looks likely to cause problems in practice.  Would it be better to behave similar to Ruby/CoffeeScript where the default constructor of a class with a super class specified is to apply the super constructor with the same arguments?  So the above would print 'Sammy the Python'.

2) Should explicit constructors without any calls to super be an error in classes with declared super classes?

Similar to the first issue above - the wiki and spec draft say that the following runs without error and prints 'undefined'.  

    class Animal { constructor() { = "hello"; } }
    class Snake extends Animal { constructor() { } }
    var sam = new Snake()

Again, it looks easy to accidentally miss making a super constructor call from an explicit derived class constructor.  There's less that can be usefully done about this case than in #1 above.  But this might be a case where a stronger static check is warranted.  Something like: report an early error when a constructor in a class with a super class specified does not include any super call.  This may have to be left to lint tools though.


More information about the es-discuss mailing list