我需要使用Q
图书馆 (http://documentup.com/kriskowal/q/ http://documentup.com/kriskowal/q/)在浏览器中。我想用RequireJS
加载这个库,但我不知道如何做到这一点。我知道如何加载我自己的模块,但我不能这样做Q
。它有一些功能:
(function (definition) {
//some another code here***
// RequireJS
} else if (typeof define === "function" && define.amd) {
define(definition);
我怎样才能加载Q
然后在另一个模块中使用它?
执行此操作的正确 AMD 方法是(借用 @Eamonn O'Brien-Strain 的示例代码):
requirejs.config({
paths: {
Q: 'lib/q'
}
});
function square(x) {
return x * x;
}
function plus1(x) {
return x + 1;
}
require(["Q"], function (q) {
q.fcall(function () {
return 4;
})
.then(plus1)
.then(square)
.then(function (z) {
alert("square of (value+1) = " + z);
});
});
这边走Q
不会泄漏到全局范围,并且很容易找到依赖于该库的所有模块。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)