Guest User

Untitled

a guest
Jun 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.43 KB | None | 0 0
  1. var A = function() {}
  2. var B = function() {}
  3.  
  4. // factory
  5. var Type = function(name) {
  6. if(name === "a") {
  7. return new A();
  8. }
  9. else {
  10. return new B();
  11. }
  12. }
  13.  
  14. // example of using the above factory
  15.  
  16. // dummy data to show example
  17. var myArray = ["a", "a", "b", "c", "b", "a"];
  18.  
  19. // loop through array
  20. for(var i = 0; i < myArray.length; i++) {
  21. // will create a new A or B depending on what the factory decides
  22. new Type(myArray[i]);
  23. }
Add Comment
Please, Sign In to add comment