【发布时间】:2013-02-10 00:55:05
【问题描述】:
我在定义函数和实例化对象的顺序上有问题,请参阅:JSFiddle
我现在只是在玩一个想法,但我碰到了这堵墙,我不知道是否有任何简单的解决方案可以解决这个问题。基本上我有一个在另一个对象上有一些方法的对象,但是这个另一个对象包含对第一个对象的引用,所以无论我实例化/定义什么顺序,我都会得到一个错误,因为一个或另一个尚未加载:
var router = {
update: function(event, from, to) {
window.location.hash = "#/" + to;
$("back-btn").disabled = fsm.can("back"); // *** And here I am referencing fsm
$("next-btn").disabled = fsm.can("next");
},
location: window.location.hash.substring(2),
}
var fsm = StateMachine.create({
initial: "intro",
events: [
// Next events and where to route based on our page
{ name: "next", from: "intro", to: "getname" },
{ name: "next", from: "getname", to: "welcome" },
{ name: "next", from: "welcome", to: "why" },
// We can't go "back" from the initial route
{ name: "back", from: "getname", to: "intro" },
{ name: "back", from: "welcome", to: "getname" },
{ name: "back", from: "why", to: "welcome" } ],
callbacks: {
onintro : router.update, //*** Here I am referencing the router object
ongetname: router.update,
onwelcome: router.update,
onwhy : router.update
}
});
感谢您的帮助。
【问题讨论】:
-
可能重复 stackoverflow.com/questions/1450997/… 另外,这里有一个关于 require.js stackoverflow.com/questions/12639772/…的好问题要检查
-
你是在 JSFiddle 的范围内做这一切,还是在其他地方进行测试?
-
我在没有 JSFiddle 作用域的浏览器中进行测试
标签: javascript