很多人都知道,Google 提供了一个 API,您可以通过调用一个简单的函数来加载特定的模块/库:
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.7.1");
google.load("jqueryui", "1.8.2");
</script>
我目前正在开发我自己的代码库,我想轻松高效地将其分发到我的网站中,我想不出比上述方法更好的方法。
但是,我不太确定为此编写代码的最佳方式是什么。显然我的库将是一个对象,所以将从这样的东西开始(我想,如果我错了请纠正我):
function company(){
this.load = function(modules){
// Modules is an array of modules to load
// Load the separate modules here
// from external files in their minified format
}
}
var company = new company;
company.load(['forms']);
上述方法是否正确?然后如何从单独的文件加载模块?
最佳答案
你可以使用 CommonJS Module规范然后你可以用 RequireJS 加载它们.
关于Javascript (jQuery) 模块/库加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12929748/