Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created March 21, 2012 16:03
Show Gist options
  • Save cowboy/2149013 to your computer and use it in GitHub Desktop.
Save cowboy/2149013 to your computer and use it in GitHub Desktop.
Finding a "safety syntax" for classes
// This code example was seen here:
// https://mail.mozilla.org/pipermail/es-discuss/2012-March/021430.html
class Horse extends Animal {
constructor(name){
super(name);
}
move() {
alert("Galloping...");
super.move(45);
}
}
// The line super.move(45); implies that super methods other than one named
// the same as the currently executing method can be invoked, eg.
// super.otherMethod(). While this is *technically* possible using Prototypal
// inheritance, it's a bad idea. The locally-defined otherMethod() should be
// responsible for determining how it delegates to its super methods, not the
// calling method.
// I recommend changing super.move(45); to super(45); like so:
class Horse extends Animal {
constructor(name){
super(name);
}
move() {
alert("Galloping...");
super(45);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment