use*_*163 6 javascript module requirejs q
我需要在浏览器中使用Q库(http://documentup.com/kriskowal/q/).我想RequireJS用来加载这个库,但我不知道如何做到这一点.我知道如何加载我自己的模块,但我不能这样做Q.它有一些功能:
(function (definition) {
//some another code here***
// RequireJS
} else if (typeof define === "function" && define.amd) {
define(definition);
Run Code Online (Sandbox Code Playgroud)
如何加载Q然后在另一个模块中使用它?
kry*_*ger 14
适当的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);
});
});
Run Code Online (Sandbox Code Playgroud)
这种方式Q不会泄漏到全局范围,并且很容易根据此库找到所有模块.
您可以使用 HTML 中的脚本语句简单地加载 Q 库
<script src="https://cdnjs.cloudflare.com/ajax/libs/q.js/1.1.0/q.js"></script>
Run Code Online (Sandbox Code Playgroud)
然后你可以通过Q变量访问它,如下所示:
function square(x) {
return x * x;
}
function plus1(x) {
return x + 1;
}
Q.fcall(function () {return 4;})
.then(plus1)
.then(square)
.then(function(z) {
alert("square of (value+1) = " + z);
});
Run Code Online (Sandbox Code Playgroud)
请参阅http://jsfiddle.net/Uesyd/1/上运行的内容
| 归档时间: |
|
| 查看次数: |
5068 次 |
| 最近记录: |