A static constructor along with static members to a class allow shared data accessible from the class itself or instances.
The Singleton and Multiton patterns are two instances where this feature can be utilized.
A static constructor takes no members and is called before any instance or static method is used. The syntax for a static member is kind of a lazy suggestion here. Ideally this proposal should wait until public, private, static member syntax is well defined.
class Multiton
{
static constructor()
{
this.instances = {}; // instances is now a static member of Multiton
// Multiton.instances = {}; // Same thing. Using "this" removes the need for using the name in the class
}
constructor(name)
{
if (this.instances.hasOwnProperty(name))
{
throw `Multiton name already used`;
}
this.instances[name] = this;
}
}
var foo = new Multiton(`foo`);
var bar = new Multiton(`bar`);
var baz = new Multiton(`bar`); // throws `Multiton name already used`
// Multiton.instances can be used to access the instances object