Javascript 编写不使用Requirejs的Requirejs模块
我想以一种方式创建我的模块,它们可以与requirejs一起使用,也可以不与requirejs一起使用(没有requirejs,它们应该正常工作,所以我必须确保它们正确加载,比如按正确的顺序放置脚本标记) 那么jQuery是这样做的:Javascript 编写不使用Requirejs的Requirejs模块,javascript,module,requirejs,Javascript,Module,Requirejs,我想以一种方式创建我的模块,它们可以与requirejs一起使用,也可以不与requirejs一起使用(没有requirejs,它们应该正常工作,所以我必须确保它们正确加载,比如按正确的顺序放置脚本标记) 那么jQuery是这样做的: // export module if ( typeof define === "function" && define.amd ) { define(["dep"], function(dep){ dep.fn.
// export module
if ( typeof define === "function" && define.amd ) {
define(["dep"], function(dep){
dep.fn.test = test;
return dep;
});
}
else{
dep.fn.test = test;
}
实际模块的定义如下所示
var dep = function(...){...}
这一定义和出口部分是在一个生命周期内,以保持一切在全球范围之外
一般来说,它运行良好,但有一个例外,即依赖项不可用。
这个问题可以通过在define部分中定义函数来解决,但这意味着在define部分中定义函数两次,在下面的else部分中定义函数两次
我如何才能让它工作,但只定义一次模块
我有一个核心dep的“插件式”扩展,这些扩展都应该在单独的文件中,因此主dep必须作为依赖项传递
顺便说一句,这很好。但这意味着我要为测试编写两次代码
(function(){
// export module
if ( typeof define === "function" && define.amd ) {
define(["dep"], function(dep){
dep.fn.test = function(){...ssomething using dep...};
return dep;
});
}
else{
dep.fn.test = unction(){...ssomething using dep...};
}
})
好吧,我再举一个例子
动画/animate.js(这是我的主文件)
动画/modules/easing.js(这是一个模块文件)
(功能(){
var ease=function(){
//使用animate.js中的animate main函数
//此处不提供动画
...
})
我想你只是写错了
define
,所以没有注册。这就是我使用的
if (typeof define === "function" && define.amd) {
define("telegraph", [], function () { return telegraph; });
}
联系上下文
(function(window) {
var telegraph = function() { };
telegraph.prototype.test = function() {
// do something
};
if (typeof define === "function" && define.amd) {
define("telegraph", [], function () { return telegraph; });
}
window.telegraph = telegraph;
})(window);
编辑
问题是如何定义test
并在内部使用dep
,这样就不必将其作为依赖项提供,并且可以定义命名的dep
模块。一个解决方案是在构造函数中注册二级函数,并将此捕获为self
(或另一个变量)在函数中使用。这里的关键是使用define定义命名模块,并且通过在构造函数中使用捕获的上下文,不需要将父对象作为依赖项提供。例如(使用working fiddle at):
实际的问题似乎是,define
在IIFE中不可用,但是窗口.define
是。因此将define作为参数传递给IIFE解决了问题
(function(define){
// export module
if ( typeof define === "function" && define.amd ) {
define(["dep"], function(dep){
dep.fn.test = function(){...ssomething using dep...};
return dep;
});
}
else{
dep.fn.test = unction(){...ssomething using dep...};
}
}(window.define))
在它检查define
之前,没有找到它并立即尝试在没有requirejs define部分的情况下将其附加到dep.fn.test。嘿,我不这么认为,因为“test”是注册的,而且通常dep是注册的,test是dep的一种方法。但是我不能在test IF(并且仅当)的函数中访问dep我定义了requirejs define语句的测试输出。如果我在其范围内定义它,则可以。如果我错了,请更详细地解释它,以便我知道在哪里可以修复某些东西。抱歉,但这没有帮助,我需要将它们放在单独的文件中,这就是我在任何情况下使用requirejs的原因。我只需要知道如何编写“缓解”函数一次,但让它尝试仅在define调用中访问主动画对象。
(function(window) {
var telegraph = function() { };
telegraph.prototype.test = function() {
// do something
};
if (typeof define === "function" && define.amd) {
define("telegraph", [], function () { return telegraph; });
}
window.telegraph = telegraph;
})(window);
(function(){
var dep = function() {
var self = this;
self.fn.test = function() {
self.foo();
};
};
dep.prototype.foo = function() {
alert('foo');
};
dep.prototype.fn = function() {
};
if ( typeof define === "function" && define.amd ) {
define('dep', [], function() { return dep; });
}
})();
(function(define){
// export module
if ( typeof define === "function" && define.amd ) {
define(["dep"], function(dep){
dep.fn.test = function(){...ssomething using dep...};
return dep;
});
}
else{
dep.fn.test = unction(){...ssomething using dep...};
}
}(window.define))