Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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]);
- }
Add Comment
Please, Sign In to add comment