Created
December 22, 2016 17:51
-
-
Save aramkoukia/7260b1dc14971c99c40b2e6def105b47 to your computer and use it in GitHub Desktop.
ES6 JavaScript static methods definition
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
class MyClass { | |
static myStaticMethod() { | |
return 'Hello'; | |
} | |
static get myStaticProperty() { | |
return 'Goodbye'; | |
} | |
} | |
// samle usage of static methods | |
console.log(MyClass.myStaticMethod()); // logs: "Hello" | |
console.log(MyClass.myStaticProperty); // logs: "Goodbye" | |
// just like you expect, Static properties are not defined on object instances: | |
const myClassInstance = new MyClass(); | |
console.log(myClassInstance.myStaticProperty); // logs: undefined | |
//However, they are defined on subclasses: | |
class MySubClass extends MyClass {}; | |
console.log(MySubClass.myStaticMethod()); // logs: "Hello" | |
console.log(MySubClass.myStaticProperty); // logs: "Goodbye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment