Last active
May 27, 2017 15:50
-
-
Save luc4leone/1120a9d497d0f9b708e8d06894b15eb6 to your computer and use it in GitHub Desktop.
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() { | |
var libraryStorage = {}; | |
function loadLibrary(libraryName, dependencyNames, addLibrary) { | |
var dependencyValues = []; | |
if (dependencyNames.length !== 0) { | |
dependencyValues = dependencyNames.map(function(dependencyName) { | |
return libraryStorage[dependencyName]; | |
}) | |
} | |
libraryStorage[libraryName] = addLibrary.apply(null, dependencyValues); | |
} | |
function requireLibrary(libraryName) { | |
return libraryStorage[libraryName]; | |
} | |
function librarySystem(libraryName, dependencyNames, addLibrary) { | |
if (arguments.length > 1) { | |
loadLibrary(libraryName, dependencyNames, addLibrary); | |
} else { | |
return requireLibrary(libraryName); | |
} | |
} | |
window.librarySystem = librarySystem; | |
})(); | |
// test | |
librarySystem('dependency', [], function() { | |
return 'loaded dependency'; | |
}); | |
librarySystem('app', ['dependency'], function(dependency) { | |
return 'app with ' + dependency; | |
}); | |
librarySystem('app'); // 'app with loaded dependency' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment