Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Last active August 29, 2015 14:05
Show Gist options
  • Save calvinmetcalf/ae24761d84a49881025b to your computer and use it in GitHub Desktop.
Save calvinmetcalf/ae24761d84a49881025b to your computer and use it in GitHub Desktop.
super in ES

super

basic

Two modes, as a function, it which case it refers to the method of the same name as where is called from but in the ancestor class of the current class

class Foo extend Bar {
   method1 () {
     super(a, b, c);
     // equivalent to Bar.prototype.method1.call(this, a, b, c);
  }
}

or it can be called as an object with methods cosponsoring to the ancestor classes method

class Baz extend Foo {
    constructor() {
    super.method1(1,2,3);
    // equivalent to Foo.prototype.method1.call(this, 1,2,3);
  }
}

advanced

from reading the spec (and using traceur) super seems to be set at class creation not at run time (though I could be wrong), e.g.

class SuperClass  {
   constructor() {
      this.what = 'msg super'
   }
   print() {
      console.log('from super', this.what);
   }
}
class ChildClass extends SuperClass {
   constructor() {
      this.what = 'msg child'
   }
   print() {
      console.log('from child', this.what);
      super();
   }
}
class OtherSuper {
   constructor() {
      this.what = 'msg other'
   }
   print() {
      console.log('from other', this.what);
   }
}
class OtherChild extends OtherSuper {
   constructor() {
      this.what = 'msg sibling'
   }
   print() {
      console.log('from sib', this.what);
      super();
   }
}

var superClass = new SuperClass();
var childClass = new ChildClass();
var otherSuper = new OtherSuper();
var otherChild = new OtherChild();

superClass.print();
// from super msg super

childClass.print();
// from child msg child
// from super msg child

otherSuper.print();
// from other msg other

otherChild.print();
// from sibling msg sibling
// from other msg sibling

childClass.print.call(otherChild);
// from child msg sibling
// from super msg sibling
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment