Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. (function() {
  2. var libraryStorage = {};
  3. function librarySystem(libraryName, dependencies, callback) {
  4.  
  5. // creating a library
  6. if(arguments.length > 1) {
  7.  
  8. // creating a dependent library
  9. if(dependencies && dependencies.length !== 0) {
  10. var requiredLibrariesStorage = [];
  11.  
  12. for(var i = 0; i < dependencies.length; i++) {
  13. requiredLibrariesStorage.push(libraryStorage[dependencies[i]]);
  14. }
  15.  
  16. libraryStorage[libraryName] = callback.apply(this, requiredLibrariesStorage)
  17. } else {
  18. // creating an independent library
  19. libraryStorage[libraryName] = callback();
  20. }
  21. } else {
  22. // retreiving a library
  23. return libraryStorage[libraryName];
  24. }
  25. }
  26. window.librarySystem = librarySystem;
  27. })();
  28.  
  29.  
  30.  
  31. librarySystem('name', [], function() {
  32. return 'Gordon';
  33. });
  34.  
  35. librarySystem('company', [], function() {
  36. return 'Watch and Code';
  37. });
  38.  
  39. librarySystem('workBlurb', ['name', 'company'], function(name, company) {
  40. return name + ' works at ' + company;
  41. });
  42.  
  43. console.log(librarySystem('workBlurb')); // 'Gordon works at Watch and Code'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement