Skip to content

Instantly share code, notes, and snippets.

@adamsilver
Created October 28, 2010 11:07
Show Gist options
  • Save adamsilver/651117 to your computer and use it in GitHub Desktop.
Save adamsilver/651117 to your computer and use it in GitHub Desktop.
var A = function() {}
var B = function() {}
// factory
var Type = function(name) {
if(name === "a") {
return new A();
}
else {
return new B();
}
}
// example of using the above factory
// dummy data to show example
var myArray = ["a", "a", "b", "c", "b", "a"];
// loop through array
for(var i = 0; i < myArray.length; i++) {
// will create a new A or B depending on what the factory decides
new Type(myArray[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment