用于指示函数参数的标准约定在 JavaScript 中未使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32197927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:58:15  来源:igfitidea点击:

Standard conventions for indicating a function argument is unused in JavaScript

javascriptnaming-conventionsunused-variables

提问by Andrew Grimm

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?

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

采纳答案by T.J. Crowder

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

为了让我们有一个可以使用的示例,这在 jQuery$.each中很常见,在这种情况下,您在迭代回调中编写不需要索引,只需要值的代码($.each相对于 向后Array#forEach):

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

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.

Using_是我见过的最接近标准方法的方法,是的,但我也见过很多其他方法——无论如何给它一个反映其目的的名称 ( index) ,称之为unused,等等。

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 classconstructs), so you have do things like _0and _1or _and __, etc.

如果您需要忽略多个参数,则不能重复相同的标识符(在严格模式下不允许的,这应该是每个人的默认值,并且是模块和class构造中的默认值),因此您可以执行_0and_1_and 之类的操作__,等等。

回答by Fredrik Carlsson

With browsers supporting destructuring one can do:

使用支持解构的浏览器可以做到:

function ({}, {}, value) {
  // console.log(value)
}

Which is kind of neat in that it avoids the problem of multiple arguments having the same name and also won't create problems with libraries that assign methods to _(lodash, underscore, etc.).

这有点巧妙,因为它避免了多个参数具有相同名称的问题,并且也不会对将方法分配给_(lodash、下划线等)的库产生问题。

One problem with this approach is that unused arguments of type undefinedor nullwill throw.

这种方法的一个问题是未使用的undefinedor类型的参数null会抛出。

For undefinedone solution is to use default parameters:

对于undefined一个解决方案是使用默认参数:

function ({}={}, {}={}, value) {
  // console.log(value)
}

Sadly no such easily applied solution for null.

遗憾的是没有这样容易应用的解决方案null

回答by Joseph Marinier

Using destructuring assignment, one can do:

使用解构赋值,可以做到:

function f(...[, , third]) {
  console.log(third);
}

f(1, 2, 3);

回答by MarkosyanArtur

I would recommend this syntax:

我会推荐这种语法:

function(_index, value) {...}

to not to shadow lodash variable and still have description of argument in case if it will be used.

不要隐藏 lodash 变量并且仍然有参数的描述,以防万一它会被使用。

VS Code is also highlight these names properly and these unused args won't be deleted after autofix code smells

VS Code 也正确突出显示了这些名称,并且在自动修复代码异味后不会删除这些未使用的参数

回答by Doug Coburn

How about using the functionargumentsobject?

如何使用functionarguments对象?

function third() { const [,,thirdArg] = arguments;
  return thirdArg;
}
console.log(third(1,2,3));