跳过回调函数中的参数 [英] Skipping parameters in callback function

查看:161
本文介绍了跳过回调函数中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的回调函数具有以下签名(来自 http://api.jquery.com/load/):

The callback function I'm working with has the following signature (from http://api.jquery.com/load/):

complete(responseText, textStatus, XMLHttpRequest)

现在,我只需要第三个参数。在Lua中有一个约定,其中下划线用于从函数中跳过不必要的返回值(跳过,因为_将实际保存该值):

Now, I only need the third parameter. In Lua there's a convention where an underscore is used to skip unneeded return values from functions (skip because _ will actually hold the value):

var1, _, _, var4 = func()

所以我想到用JavaScript做类似的事情,并将我的函数签名设置为:

So I thought of doing a similar thing with JavaScript and set my function signature to this:

function (_, _, XMLHttpRequest)

也许有更好的/更清洁的方式?

Is there anything wrong with this approach, perhaps there's a better/cleaner way?

推荐答案

技术不漂亮,但我自己使用它在几个场合。我想它仍然是更好的给那些未使用的参数有意义的名字(只是为了避免混淆),但你很好使用下划线。

The technique is not pretty, but I use it myself on several occasions. I guess it is still quite better to give those unused arguments meaningful names (just to avoid confusion), but you're fine in using underscores.

我经常看到它使用在jQuery相关的回调,其中索引通常作为第一个参数传递,如

I often see it used in jQuery related callbacks, where the index is often passed in as first argument, like

$('.foo').each(function(_, node) {
});

因为大多数时候,你不关心索引。所以要回答你的实际问题,使用该技术没有什么错误(除了混乱可能),没有更好的/更清洁的方式来跳过不需要的参数。

because most of the time, you don't care about the index there. So to answer your actual question, there is nothing wrong in using the technique (beside confusion maybe) and there is no better/cleaner way to skip unwanted arguments.

这篇关于跳过回调函数中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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