javascript javascript函数参数中的下划线是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27637013/
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 07:45:36  来源:igfitidea点击:

What is the meaning of an Underscore in javascript function parameter?

javascript

提问by Azhar

I was going through the code of one of the chart library written in javascript, wherein I've seen passing underscore(_) as a function parameter. What does that mean?

我正在浏览用 javascript 编写的图表库之一的代码,其中我看到将下划线(_)作为函数参数传递。这意味着什么?

e.g.

例如

chart.x = function(_) {
    if (!arguments.length) return lines.x;
    lines.x(_);
    lines2.x(_);
    return chart;
  };

Can someone please update on this...Thanks.

有人可以更新这个...谢谢。

回答by sagar43

The underscore symbol _is a valid identifier in JavaScript, and in your example, it is being used as a function parameter.

下划线符号_是 JavaScript 中的有效标识符,在您的示例中,它被用作函数参数。

A single underscore is a convention used by some javascript programmers to indicate to other programmers that they should "ignore this binding/parameter". Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely.

单个下划线是一些 JavaScript 程序员用来向其他程序员表明他们应该“忽略此绑定/参数”的约定。由于 JavaScript 不进行参数计数检查,因此可以完全省略参数。

This symbol is often used (by convention again) in conjunction with fat-arrow functions to make them even terser and readable, like this:

这个符号通常(再次按照惯例)与粗箭头函数结合使用,使它们更简洁易读,如下所示:

const fun = _ => console.log('Hello, World!')
fun()

In this case, the function needs no params to run, so the developer has used the underscore as a convention to indicate this. The same thing could be written like this:

在这种情况下,该函数不需要参数来运行,因此开发人员使用下划线作为约定来表示这一点。同样的事情可以这样写:

const fun = () => console.log('Hello, World!')
fun()

The difference is that the second version is a function with no parameters, but the first version has a parameter called _ that is ignored. These are different though and the second version is safer, if slightly more verbose (1 extra character).

不同的是,第二个版本是一个没有参数的函数,但第一个版本有一个名为 _ 的参数被忽略了。这些是不同的,第二个版本更安全,如果稍微更冗长(1 个额外字符)。

Also, consider a case like

另外,考虑一个案例

arr.forEach(function (_, i) {..})

Where _indicates the first parameter is not to be used.

where_表示不使用第一个参数。

The use of underscores like this can get very confusing when using the popular lodash or underscore libraries.

在使用流行的 lodash 或下划线库时,像这样使用下划线会变得非常混乱。

回答by Anand Raja

_ in fat arrow function is called as throwaway variable. It means that actually we're creating an variable but simply ignoring it. More devs are now a days using this as syntactic sugar or short hand while writing code, as it's easy and one character less to write the code.

_ 在胖箭头函数中被称为一次性变量。这意味着实际上我们正在创建一个变量,但只是忽略了它。现在越来越多的开发人员在编写代码时使用它作为语法糖或速记,因为编写代码很容易并且少一个字符。

Instead of using _, you can use other variables like temp, x, etc

除了使用 _,您还可以使用其他变量,如 temp、x 等

for examples:

举些例子:

() => console.log('Hello World')



_ => console.log('Hello World')




x => console.log('Hello World')

But personally i prefer to use () type over throwaway variable if no arguments are needed.

但我个人更喜欢在不需要参数的情况下使用 () 类型而不是一次性变量。

See the following code, then you will understand it better.

看下面的代码,你会更好地理解它。

_ as an argument,

_ 作为论据,

  f = _=> {
    return _ + 2 ;
}

f(3) will return 5

f(3) 将返回 5

For better understanding, check wes bos

为了更好地理解,请查看 we bos