Guest User

Untitled

a guest
Jun 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. (function() {
  2. var libraryStorage = {};
  3.  
  4. // librarySystem now takes three arguments, including dependencyNames
  5. function librarySystem(libraryName, dependencyNames, callback) {
  6. if (arguments.length > 1) { // Create case
  7. var dependencies = [];
  8. // For each dependency name, use librarySystem to return the
  9. // corresponding library and push it to the dependencies array.
  10. dependencyNames.forEach(function(name) {
  11. dependencies.push(librarySystem(name));
  12. });
  13. // Unpack dependencies into callback arguments using spread (...) syntax
  14. libraryStorage[libraryName] = callback(...dependencies);
  15. } else { // Use case
  16. return libraryStorage[libraryName];
  17. }
  18. }
  19.  
  20. window.librarySystem = librarySystem;
  21.  
  22. })();
  23.  
  24. // Usage:
  25. // 1. Create our libraries:
  26. librarySystem('name', [], function() {
  27. return 'Gordon';
  28. });
  29.  
  30. librarySystem('company', [], function() {
  31. return 'Watch and Code';
  32. });
  33.  
  34. librarySystem('workBlurb', ['name', 'company'], function(name, company) {
  35. return name + ' works at ' + company;
  36. });
  37.  
  38. // 2. Ask librarySystem to return our 'workBlurb' library, which depends on
  39. // the 'name' and 'company' libraries.
  40.  
  41. librarySystem('workBlurb'); // 'Gordon works at Watch and Code'
Add Comment
Please, Sign In to add comment