javascript static methods code example
Example 1: js class static function
class myClass{
constructor(){
this.myLocaleVariable=1;
}
localfunction(){
return "im local unique to this variable";
}
static publicfunction(){
return "i can be called without an obj"
}
}
myClass.myPublicVariable = 0;
myClass.localfunction();
myClass.publicfunction();
myClass.myLocaleVariable;
myClass.myPublicVariable;
var obj = new myClass;
obj.localfunction();
obj.publicfunction();
obj.myLocaleVariable;
obj.myPublicVariable;
Example 2: js class static
class ClassWithStaticMethod {
static staticMethod() {
return 'static method has been called.';
}
}
console.log(ClassWithStaticMethod.staticMethod());
Example 3: js static methods
class Foo(){ static methodName(){ console.log("bar") }}
Example 4: get static methods of class javascript
class Hi {
constructor() {
console.log("hi");
}
my_method(){}
static my_static_method() {}
}
function getStaticMethods(cl) {
return Object.getOwnPropertyNames(cl)
}
console.log(getStaticMethods(Hi))