鉴于 javascript 应用程序,我有此代码来设置“状态机”:
var Events = {
bind: function(){
if ( !this.o ) this.o = $({});
this.o.bind(arguments[0], arguments[1])
},
trigger: function(){
if ( !this.o ) this.o = $({});
this.o.trigger(arguments[0], arguments[1])
}
};
var StateMachine = function(){};
StateMachine.fn = StateMachine.prototype;
$.extend(StateMachine.fn, Events);
StateMachine.fn.add = function(controller){
this.bind("change", function(e, current){
console.log(current);
if (controller == current)
controller.activate();
else
controller.deactivate();
});
controller.active = $.proxy(function(){
this.trigger("change", controller);
}, this);
};
var con1 = {
activate: function(){
console.log("controller 1 activated");
},
deactivate: function(){
console.log("controller 1 deactivated");
}
};
var sm = new StateMachine;
sm.add(con1);
con1.active();
我现在不明白的是绑定函数中的当前参数来自哪里(即:)。我尝试将它记录在 firebug 控制台面板上,它似乎是 StateMachine.fn.add 函数中的控制器参数。你能告诉我这个参数是从哪里来的吗?谢谢你。this.bind("change", function(e, current){...}