【发布时间】:2013-02-14 00:27:26
【问题描述】:
我从这里得到了一些 AMD/CommonJS 适配器代码: supporting both CommonJS and AMD
(function (name, definition) {
if (typeof module != 'undefined') {
module.exports = definition();
}
else if (typeof define == 'function' && typeof define.amd == 'object') {
define(name, [], definition);
}
else {
this[name] = definition();
}
}('modXyz', {
sayHi:function (name) {
console.log('Hi ' + name + '!');
}
}
));
我想将该代码与 Curl 一起使用,以使我的所有代码与 AMD/CommonJS 兼容。我期望能够做到的是:
greeter = curl(['modXyz']);
greeter.sayHi("Gracie");
但是curl 返回的对象不是我期望的对象。我能得到的最接近的是:
curl(['modXyz'], function(mod) { window.greeter = mod; });
greeter.sayHi("Gracie");
这似乎违背了 AMD 的目的。 curl 有能力做这样的事情吗?我必须使用require.js 来实现它吗?
【问题讨论】:
标签: javascript module requirejs amd curl.js