Created
June 17, 2019 06:46
-
-
Save jasontwuk/9fd6468838d2c5c4066502eb85af755f to your computer and use it in GitHub Desktop.
Watch and Code - Beasts 4. librarySystem with dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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