这个问题在这里已有答案:
鉴于:
StringPreconditions
延伸ObjectPreconditions
ObjectPreconditions
依赖于StringPreconditions
(其中一个方法返回子类)Preconditions
是ObjectPreconditions
和StringPreconditions
的守门人(确保他们在返回实例之前加载)User
依赖于Preconditions
我有这个代码:
define(["ObjectPreconditions"], function(ObjectPreconditions)
{
console.log("Inside StringPreconditions");
function StringPreconditions() {}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
return StringPreconditions;
});
define(["require"], function(require)
{
console.log("Inside ObjectPreconditions");
// Resolve circular dependencies
var StringPreconditions;
require(["StringPreconditions"], function(theStringPreconditions)
{
StringPreconditions = theStringPreconditions;
console.log("ObjectPreconditions finished loading StringPreconditions");
});
function ObjectPreconditions() {}
ObjectPreconditions.prototype.isInstanceOf(type)
{
console.log("ObjectPreconditions.isInstanceOf() invoked");
if (type === String)
return new StringPreconditions();
}
});
define(["ObjectPreconditions", "StringPreconditions"], function(ObjectPreconditions, StringPreconditions)
{
console.log("Inside Preconditions");
var Preconditions = {};
Preconditions.requireThat(parameter) = function()
{
return new ObjectPreconditions(parameter);
};
return Preconditions;
});
define(["Preconditions"], function(Preconditions)
{
console.log("Inside User");
function User() {}
User.prototype.doSomething = function()
{
var StringPrecondition = Preconditions.requireThat("test").isInstanceOf(String);
}
});
问题是我有10%的时间得到这个加载顺序:
我已经阅读过http://requirejs.org/docs/api.html#circular,但我相信他们正在做同样的事情。
有任何想法吗?
更新:https://stackoverflow.com/a/42264822/14731包含ES6模块的更新答案。
我想通了:我们需要创建一个“关守”文件,它将定义依赖于循环依赖关系的函数。
ObjectPreconditions.js
重命名为AbstractObjectPreconditions.js
。ObjectPreconditions.js
文件(我们的新网守)。AbstractObjectPreconditions.js
移到ObjectPreconditions.js
require(ObjectPreconditions)
。循环依赖中涉及的代码(例如子类)应该是require(AbstractObjectPreconditions)
。以下是生成的代码:
define(["AbstractObjectPreconditions"], function(ObjectPreconditions)
{
console.log("Inside StringPreconditions");
function StringPreconditions() {}
StringPreconditions.prototype = Object.create(ObjectPreconditions.prototype);
StringPreconditions.prototype.constructor = ObjectPreconditions;
return StringPreconditions;
});
define(["require"], function(require)
{
console.log("Inside AbstractObjectPreconditions");
function ObjectPreconditions() {}
return ObjectPreconditions;
});
define(["AbstractObjectPreconditions"], function(ObjectPreconditions)
{
// Gatekeeper for circular dependencies
ObjectPreconditions.prototype.isInstanceOf(type)
{
console.log("ObjectPreconditions.isInstanceOf() invoked");
if (type === String)
return new StringPreconditions();
}
return ObjectPreconditions;
});
define(["ObjectPreconditions", "StringPreconditions"], function(ObjectPreconditions, StringPreconditions)
{
console.log("Inside Preconditions");
var Preconditions = {};
Preconditions.requireThat(parameter) = function()
{
return new ObjectPreconditions(parameter);
};
return Preconditions;
});
define(["Preconditions"], function(Preconditions)
{
console.log("Inside User code");
function User() {}
User.prototype.doSomething = function()
{
var StringPrecondition = Preconditions.requireThat("test").isInstanceOf(String);
}
});