如何在函数引用中传递参数

我正在寻找一种方法来保存对jQuery中事件触发器之后调用的一个函数中的两个this对象的引用 – this对该方法所定义的对象的引用(所以我可以使用this.anotherObjectFunction() )和this引用触发事件的对象 – 以便我以后可以使用$(this).someJQueryFunction 。 我想这样做的方法是将this (function object)引用作为参数传递给函数。 不幸的是,这个函数是由jQuery调用的,而不是我,所以它作为参考传递,即

 someFunction: function() { ... cell.$el.on('click', 'li.multiselect-option', this.myClickFunction); ... }, myClickFunction: function(objectReference) { //It should be able to call methods of that object. objectReference.anotherFunction(); //And reference to the clicked item. $(this).html("Don't click me anymore!!!"); } 

我知道我可以做类似的事情

 cell.$el.on('click', 'li.multiselect-option', myFunction.bind(this)); ... myClickFunction: function(event) { this.anotherFunction(); $(event.currentTarget).html("Don't click me anymore!!!"); } 

但是这个解决方法并没有真正回答这个问题,因为它没有显示如何传递额外的参数,并且将来可能有必要传递另一个(不,我不想将它们注册为对象中的字段) 。

除非可以使用cell.$el.off()轻松删除它们,否则不允许使用匿名函数cell.$el.off()函数将删除它们并且删除它们(还有一些其他函数同时与相同的对象和事件关联,它们应保持不变)。

更新:

没有匿名function,我的意思是:

 var self = this; cell.$el.on('click', 'li.multiselect-option', function() { self.MyClickFunction(self, this); }); 

它们不起作用,因为我必须使用带有函数引用 (3参数原型)的cell.$el.off()来删除这个单独的函数而只​​删除它,将其他函数绑定到同一个元素和事件。

Jquery .on事件可以选择将参数作为参数传递给事件处理程序,就像这样

 cell.$el.on('click', 'li.multiselect-option', {arg1:'arg1' , arg2:'arg2'} , myFunction); ... myClickFunction: function(event) { alert(event.data.arg1); alert(event.data.arg2); this.anotherFunction(); $(event.currentTarget).html("Don't click me anymore!!!"); } 

将数据传递给处理程序

如果向.on()提供了数据参数,并且该参数不为null或未定义,则每次触发事件时都会将其传递给event.data属性中的处理程序。 data参数可以是任何类型,但如果使用字符串,则必须提供选择器或将其显式传递为null,以便数据不会被误认为是选择器。 最佳做法是使用普通对象,以便可以将多个值作为属性传递。

或另一种方式

 cell.$el.on('click', 'li.multiselect-option', function() { myClickFunction("hai" , "bye"); }); myClickFunction: function(arg1, arg2) { alert(arg1); alert(arg2); } 

我还建议一个无插件的解决方案来解决“如何为函数引用提供参数”的问题

 function x(a){ console.log(a); } setInterval('x(1)',1000);