Created
October 6, 2017 16:49
-
-
Save notalentgeek/93343e519de6f28f6043b75b364036b0 to your computer and use it in GitHub Desktop.
JavaScript Anyton (Singleton, Doubleton, X-ton)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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