Last active
December 20, 2016 20:46
-
-
Save gordonmzhu/c6a85cad30ab2f645502ef1fb471ac49 to your computer and use it in GitHub Desktop.
Simple Library Loader
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 librarySystem(libraryName, callback) { | |
// if (arguments.length > 1) { | |
// libraryStorage[libraryName] = callback(); | |
// } else { | |
// return libraryStorage[libraryName]; | |
// } | |
// } | |
// window.librarySystem = librarySystem; | |
// })(); | |
(function() { | |
var libraryStorage = {}; | |
function librarySystem(libraryName, dependencies, callback) { | |
if (arguments.length > 1) { | |
if (dependencies.length > 0) { | |
var dependenciesArray = []; | |
dependencies.forEach(function(dependency) { | |
dependenciesArray.push(libraryStorage[dependency]); | |
}); | |
} | |
libraryStorage[libraryName] = callback.apply(null, dependenciesArray); | |
} else { | |
return libraryStorage[libraryName]; | |
} | |
} | |
window.librarySystem = librarySystem; | |
})(); | |
librarySystem('router', [], function() { | |
return { | |
description: 'Supposedly, I can handle app routing.', | |
dependencies: [arguments] | |
}; | |
}); | |
librarySystem('app', ['router'], function(router) { | |
return { | |
description: 'Just a silly app that has a dependency', | |
dependencies: [arguments] | |
}; | |
}); | |
console.log(librarySystem('app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment