Created
March 21, 2012 16:03
-
-
Save cowboy/2149013 to your computer and use it in GitHub Desktop.
Finding a "safety syntax" for classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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