Skip to content

Instantly share code, notes, and snippets.

@notalentgeek
Created October 6, 2017 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notalentgeek/93343e519de6f28f6043b75b364036b0 to your computer and use it in GitHub Desktop.
Save notalentgeek/93343e519de6f28f6043b75b364036b0 to your computer and use it in GitHub Desktop.
JavaScript Anyton (Singleton, Doubleton, X-ton)
var anyton = function (ton, parameters, init_count) {
if (!init_count) {
init_count = 1; // Singleton.
}
var instances = [];
this.create_instance = function () {
if (instances.length < init_count) {
instances.push(new ton(...parameters)); // No pun intended.
}
return instances[instances.length - 1];
};
};
var dummy_object = function () {};
dummy_object = new anyton(dummy_object, []);
var a = dummy_object.create_instance();
var b = dummy_object.create_instance();
console.log(a === b);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment