static class in es6 code example
Example 1: typescript static class equivalent
export abstract class Tools {
public static truncate(str, length) {
var ending = '...';
if (str.length > length) {
return str.substring(0, length - ending.length) + ending;
} else {
return str;
}
};
}
Example 2: 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;