Skip to content

Instantly share code, notes, and snippets.

@jasontwuk
Created June 17, 2019 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasontwuk/9fd6468838d2c5c4066502eb85af755f to your computer and use it in GitHub Desktop.
Save jasontwuk/9fd6468838d2c5c4066502eb85af755f to your computer and use it in GitHub Desktop.
Watch and Code - Beasts 4. librarySystem with dependencies
(function(){
// build a storage for libraries
var libraryStorage = {};
function librarySystem(libraryName, dependencyArray, callback){
// when has dependencyArray and callback
if(arguments.length > 1){
// when has dependency
if(dependencyArray.length > 0){
// save the content of dependency in an array
var contentOfDependency = dependencyArray.map(function(x){return librarySystem(x)});
// pass content of dependency as callback's argument
// save the library's name and content in the libraryStorage
libraryStorage[libraryName] = callback.apply(this, contentOfDependency);
// when no dependency
} else {
// save the library's name and content in the libraryStorage
libraryStorage[libraryName] = callback();
}
// when no dependencyArray and callback
} else {
// return library's content
return libraryStorage[libraryName];
}
}
// make librarySystem accessable globally
window.librarySystem = librarySystem;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment