Javascript 绑定回调参数
我有以下代码来设置javascript应用程序的“状态机”:Javascript 绑定回调参数,javascript,jquery,Javascript,Jquery,我有以下代码来设置javascript应用程序的“状态机”: var Events = { bind: function(){ if ( !this.o ) this.o = $({}); this.o.bind(arguments[0], arguments[1]) }, trigger: function(){ if ( !this.o ) this.o = $({});
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();
现在我不明白的是bind函数中的current参数来自何处(即:this.bind(“change”,function(e,current){…}
)。我尝试将其记录在firebug控制台面板上,它似乎是StateMachine.fn.add函数中的控制器参数。您能告诉我此参数来自何处吗?
谢谢。据我所知,您在此处指定了要传递给您的事件回调的第二个参数:
this.trigger("change", controller);
jQuery的触发器方法将调用所有绑定函数,将事件对象作为第一个参数(始终)传递,然后在它之后,将所有参数传递给.trigger()事件名称后的方法。这将是
控制器
,因为它是在触发时传递给处理程序
参数的!您能解释一下吗,我不明白您的意思。