简体   繁体   English

javascript传递函数作为参数

[英]javascript passing function as parameter

If I have this: 如果我有这个:

$(SomeID).on({
    click: function () { SomeFunction1(); },
    mouseenter: function () { SomeFunction2(); },
    mouseleave:function () { SomeFunction3(); }
}, '.SomeClass');

I can rewrite it as 我可以把它重写为

$(SomeID).on({
    click: SomeFunction1,
    mouseenter: SomeFunction2,
    mouseleave: SomeFunction3
}, '.SomeClass');

But what if I need to pass some parameter like this: 但是,如果我需要传递这样的参数怎么办:

$(SomeID).on({
    click: function () { SomeFunction1($(this)); },
    mouseenter: function () { SomeFunction2($(this).index()); },
    mouseleave: function () { SomeFunction3($(this).index()); }
}, '.SomeClass');

Is there an alternative? 还有其他选择吗?

Thanks. 谢谢。

As @Jashwant says, the same this would be used in the function anyway, so it's the one value you don't need to worry about (in your example). 作为@Jashwant说,同样this将在功能中使用,无论如何,所以它你不必担心(在你的例子)的一个值。

Note that you could do as you describe if you needed to, it's easy for static values, and is called currying . 请注意,如果需要,可以按照自己的描述进行操作,静态值很容易,并称为currying A javascript example would be: http://www.dustindiaz.com/javascript-curry/ 一个javascript示例是: http//www.dustindiaz.com/javascript-curry/

You should modify implementation of SomeFunction s to get them work without parameter. 您应该修改SomeFunction的实现,以使它们在没有参数的情况下工作。

For example, if you have: 例如,如果您有:

function SomeFunction2(arg) {
  //do something assuming arg to be $(this).index()
}

You can write it like that: 你可以这样写:

function SomeFunction2() {
  var arg = $(this).index();
  //do exactly the same
}

After doing that for all three callbacks, you can use your second code sample to bind them. 在对所有三个回调执行此操作后,您可以使用第二个代码示例来绑定它们。

The meaning of this inside a javascript function does not depend on the lexical scope the function was defined in – for example, the following alerts "Hello, World!", event if this.name is not defined when greet is created 的意思this一个javascript函数内部不依赖于词法范围的功能被定义-例如,下面的提示“你好,世界!”,如果事件this.name没有定义时, greet创建

var x = { name: 'World' };
var greet = function() { alert('Hello, ' + this.name); };
x.greet = greet;
x.greet();

the following too alerts "Hello, World!": 以下也警告“Hello,World!”:

var x = { name: 'World' };
var y = { name: 'Dude', greet: function() { alert('Hello, ' + this.name); } };
x.greet = y.greet;
x.greet();

Behind the scenes, what goes on is similar to: 在幕后,发生的事情类似于:

var greet = function() { alert('Hello, ' + this.name); };
greet.call({ name: 'World' });

So you can safely mix your #2 and #3 snippets. 因此,您可以安全地混合#2和#3片段。

BTW: BTW:

most jQuery event handlers get called with a reference to the jQuery event object as the first parameter, so if you find how this works tricky (or if you fear you'll have to explain to each one of you colleagues), you can also use event.delegateTarget instead of this . 大多数jQuery事件处理程序都是通过引用jQuery事件对象作为第一个参数来调用的,所以如果你发现this工作方式很棘手(或者如果你担心你必须向每个同事解释),你也可以使用event.delegateTarget而不是this

See for example: 参见例如:

$(document).click(function(evt){ alert (evt.delegateTarget === this); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM