简体   繁体   English

javascript函数参数中下划线的含义是什么?

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

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.我正在浏览一个用 javascript 编写的图表库的代码,其中我看到将下划线( _ )作为函数参数传递。 What does that mean?这意味着什么?

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

Can someone please update on this...Thanks.有人可以请更新这...谢谢。

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".单个下划线是一些 JavaScript 程序员用来向其他程序员表明他们应该“忽略此绑定/参数”的约定。 Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely.由于 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.其中_表示不使用第一个参数。

The use of underscores like this can get very confusing when using the popular lodash or underscore libraries.在使用流行的 lodash 或下划线库时,像这样使用下划线会变得非常混乱。

_ 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM