指示函数参数的标准约定未在JavaScript中使用 [英] Standard conventions for indicating a function argument is unused in 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屋!