Zmienna statyczna JavaScript w klasie
class Thing {
static type = 'thing';
static myType() {
return `This class has a type of ${this.type}`;
}
}
console.log(Thing.type);
//=> 'thing'
console.log(Thing.myType());
//=> 'This class has a type of thing'
// Instances do not inherit static fields and methods
const t = new Thing();
console.log(t.type);
//=> undefined
console.log(t.myType())
//=> Uncaught TypeError: t.myType is not a function
Famous Flatworm