【发布时间】:2014-06-30 15:21:35
【问题描述】:
我正在尝试在我的非 AMD 要求应用程序中使用库 -- Google's libphonenumber。吃这个的最好方法是什么?我知道我可以创建这样的模块:
define(['module'], function (module) {
// insert and return library code here.
});
但这似乎不太好。似乎我必须重构他们的一些代码才能使其正常工作(例如,将其全部转换为对象并返回该对象)。我看到很多库使用不同的模式,它们使用立即调用的函数,该函数在窗口对象上定义模块并返回它。
(function() {
var phoneformat = {};
window.phoneformat = phoneformat;
if (typeof window.define === "function" && window.define.amd) {
window.define("phoneformat", [], function() {
return window.phoneformat;
});
}
})();
** 更新 ** 有什么理由不这样做吗?
define(['lib/phoneformatter'], function(phoneformatter) {
});
我可以访问我的所有方法,但现在它们似乎是全局的,因为我没有将库包装在定义中......
【问题讨论】:
标签: javascript requirejs js-amd libphonenumber