如何在函数引用中传递参数 [英] How to pass arguments in a function reference

查看:128
本文介绍了如何在函数引用中传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm looking for a way to save reference to two this objects in one function called after an event triggers in jQuery - this reference to the object the method is defined in (so I can use this.anotherObjectFunction()) and this reference to the object that triggered the event - so that I can use $(this).someJQueryFunction later on. The way I'd like to do it is by passing a this (function object) reference as an argument to the function. Unfortunately, the function is to be called by jQuery, not me, so it's passed as a reference, i.e.

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!!!");
}

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

I'm aware of the fact that I can do something like

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

但是此解决方法并不能真正回答问题,因为它没有显示如何传递其他参数,并且将来可能需要传递另一个参数(不,我不想将它们注册为对象).

But this workaround doesn't really answer the question as it doesn't show how to pass additional arguments and in the future there may be a necessity to pass another (no, I don't want to register them as fields in the object).

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

No anonymous functions are allowed unless they can be easily removed with cell.$el.off() function that will remove them and only them (there are some other function associated with the same objects and events at the same time and they should remain intact).

更新:

没有匿名功能,我的意思是像这样的解决方案:

By no anonymous functions I mean solutions like:

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

它们将不起作用,因为我必须将cell.$el.off()功能参考(三参数原型)一起使用,才能删除该单个功能,而只能删除它,将其他功能绑定到相同的元素和事件.

They will not work because I'll have to use cell.$el.off() with the function reference (3-argument prototype) to remove this single function and only it, leaving other functions bound to both the same element and event.

推荐答案

jQuery .on事件可以选择将参数作为事件处理程序中的参数传递

Jquery .on event has option to pass the argument as parameter in event handler like this

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,以便不会将数据误认为选择器.最佳做法是 使用普通对象,以便可以将多个值传递为 属性.

If a data argument is provided to .on() and is not null or undefined, it is passed to the handler in the event.data property each time an event is triggered. The data argument can be any type, but if a string is used the selector must either be provided or explicitly passed as null so that the data is not mistaken for a selector. Best practice is to use a plain object so that multiple values can be passed as properties.

或其他方式

cell.$el.on('click', 'li.multiselect-option',  function() { 

           myClickFunction("hai" , "bye");

});


myClickFunction: function(arg1, arg2)
{
   alert(arg1);

   alert(arg2);   
}

这篇关于如何在函数引用中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆