我对定义函数和实例化对象的顺序有疑问,请参阅: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
}
});
谢谢你的帮助。