Skip to content

Instantly share code, notes, and snippets.

@yookoala
Created February 17, 2022 07:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yookoala/a8047018df3ecca8202a493c902aab8f to your computer and use it in GitHub Desktop.
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
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