指示函数参数的标准约定未在JavaScript中使用 [英] Standard conventions for indicating a function argument is unused in JavaScript

查看:108
本文介绍了指示函数参数的标准约定未在JavaScript中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何标准方法将函数参数标记为JavaScript中未使用的,类似于在Ruby中用下划线启动方法参数?

Are there any standard ways of marking a function argument as unused in JavaScript, analogous to starting a method argument with an underscore in Ruby?

推荐答案

仅提供一个示例,这与jQuery的$.each相当普遍,您在其中编写不需要索引的代码,只是值,在迭代回调中($.each相对于Array#forEach向后):

Just so we have an example to work from, this is fairly common with jQuery's $.each where you're writing code that doesn't need the index, just the value, in the iteration callback ($.each is backward relative to Array#forEach):

$.each(objectOrArrayLikeThing, function(_, value) { }
    // Use value here
});

使用_是我所见过的最接近标准方法的方法,是的,但是我也看到很多其他方法.为它起一个能反映其用途的名称(index),将其命名为unused,等等.

Using _ is the closest I've seen to a standard way to do that, yes, but I've also seen lots of others — giving it a name reflective of its purpose anyway (index), calling it unused, etc.

如果您需要忽略多个参数,则不能重复相同的标识符(它是

If you need to ignore more than one parameter, you can't repeat the same identifier (it's disallowed in strict mode, which should be everyone's default and is the default in modules and class constructs), so you have do things like _0 and _1 or _ and __, etc.

这篇关于指示函数参数的标准约定未在JavaScript中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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