super() call in methods

Bergi a.d.bergi at web.de
Tue Dec 16 13:18:55 PST 2014


Hi!
I've just read the spec draft 
<http://people.mozilla.org/~jorendorff/es6-draft.html> on the super 
keyword to answer a StackOverflow question 
<http://stackoverflow.com/a/27511897/1048572>.

 From what I understood, it's currently not possible call a super method 
using only `super()`, contrary to common (my?) intution. A quick web 
search shows that other people think similar:

http://www.2ality.com/2011/11/super-references.html (with a 2013 update):
| Super-references (including super-calls) are a feature of ECMAScript | 
6 which allows one to write describe() much more succinctly:
|
|     Employee.prototype.describe = function () {
|         // super() is an abbreviation of super.describe()
|         return super()+" ("+this.title+")";
|     };
|
| Although they look similar, super and this are independent features.
| super means “I’m currently in a method – find the method that that
| method has overridden and apply it to the same instance that is
| presently active (i.e., this stays the same)”

http://javascriptplayground.com/blog/2014/07/introduction-to-es6-classes-tutorial/:
| class LogView extends View {
|   render() {
|     var compiled = super();
|     console.log(compiled);
|   }
| }
|
| We first call super(). This calls the parent class' render() method,
| and returns the result. This means that the render method on the View
| class is first called, and the result is stored in the compiled
| variable.

https://esdiscuss.org/topic/base-value-of-super-reference-indicates-the-prototype-object-of-the-current-derived-class:
| class Base {
|   say() {
|     console.log('base');
|   }
| };
|
| class Derived extends Base {
|   say() {
|     console.log('derived');
|     super();
|   }
| };
|
| let instance = new Derived;
| instance.say();

I've read <https://esdiscuss.org/topic/referencing-super> and it seems 
that needing to call `super.describe()`/`super.render()`/`super.say()` 
is intended behaviour. I'm fine with that, as explicit is better than 
implicit and "finding the method with the same *name*" (or something 
like that) is overly complicated and maybe even ambiguous.

However, it seems that we need to communicate better that `super()` 
calls only work in constructors, and other functions that inherit from 
functions 
(<http://people.mozilla.org/~jorendorff/es6-draft.html#sec-getsuperconstructor>). 
That seems to be the reason why the method name GetSuper*Constructor*() 
was chosen in the spec.
Yet, `super()` calls in plain methods would actually work! The method 
objects would inherit from `Function.prototype`, which is itself 
callable: a no-op function. This might lead to subtle bugs, where 
`super()` was intended to call the parent class's method, but does 
nothing - not even throwing an error!

Should an exception been thrown if the `func` returned by 
`GetSuperConstructor()` is `%FunctionPrototype%`?

Best regards,
  Bergi

PS: there's a typo in `GetSuperConstructor`: s/activeFuntion/activeFunction/


More information about the es-discuss mailing list