Created
February 17, 2022 07:44
-
-
Save yookoala/a8047018df3ecca8202a493c902aab8f to your computer and use it in GitHub Desktop.
A ES6 demonstration of how to call static method in non-static class method
This file contains 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
class DummyParent { | |
static hello() { | |
console.log('dummy parent say hello') | |
} | |
} | |
class Dummy extends DummyParent { | |
static foo() { | |
console.log('dummy say foo') | |
} | |
/** | |
* Demonstrate how to call static method | |
* in ordinary method. | |
*/ | |
sayHello() { | |
// Calling static method of this class. | |
this.constructor.foo() | |
// Calling static method of its parent class. | |
this.constructor.hello() | |
// Say something anyway. | |
console.log('dummy say hello') | |
} | |
} | |
const d = new Dummy(); | |
// Calling an ordinary method. | |
d.sayHello() | |
// Calling the static class of parent method | |
// by the children class. | |
Dummy.hello() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment