下划线作为 JavaScript 变量?

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

Underscore as a JavaScript variable?

javascript

提问by Siphiwe Gwebu

In this post, Nick Craverprovided an answer in which he used:

这篇文章中Nick Craver提供了一个他使用的答案:

function(_, id)

This code doesn't declare the underscore as a variable before using it. My search on google and on here only points to references to the use of the underscore as a prefix., not as the variable itself. What does it do? I like the solution by Nick but that bit bothers me.

此代码在使用下划线之前不会将其声明为变量。我在 google 和 here 上的搜索仅指向使用下划线作为前缀的引用,而不是作为变量本身。它有什么作用?我喜欢尼克的解决方案,但这让我很困扰。

回答by Ivan

I've seen the underscore used to denote that the variable is a "don't care" variable. It means that it doesn't matter and isn't used at all.

我已经看到下划线用来表示该变量是一个“无关”变量。这意味着它无关紧要,根本不使用。

In the case you pointed out, it was used to signify that his function had two arguments, but he only needed the second one.

在您指出的情况下,它用于表示他的函数有两个参数,但他只需要第二个参数。

回答by nhahtdh

Underscore is a valid JS variable name. The parameter named _in the above example can be used as any other variable.

下划线是一个有效的 JS 变量名_上例中命名的参数可以用作任何其他变量。

However, it is usually used to indicate to subsequent (human) reader of the code that whatever passed in will not be used. (The author of the code can be evil/ignorant and use it in the function, though).

但是,它通常用于向代码的后续(人类)阅读者表明传入的任何内容都不会被使用。(不过,代码的作者可能是邪恶的/无知的,并在函数中使用它)。

回答by Sarath Chandra

As a beginner, I failed to gather the many aspects associated with this answer, which are scattered over the comments and other answers. So, I'll try to consolidate them below:

作为初学者,我未能收集与此答案相关的许多方面,这些方面分散在评论和其他答案中。因此,我将尝试在下面合并它们:

Firstly, in the mentioned piece of code, the underscore argument is used as follows:-

首先,在提到的一段代码中,下划线参数的使用如下:

$(this).val('').attr('id', function(_, id) { return id + i });

From the jQuery documentation for the attrfunction here, there exists an overloaded form of attrwhich is .attr( attributeName, function ). The functionin this form is described as

这里attr函数的 jQuery 文档中,存在一个重载形式是。在以这种形式被描述为attr.attr( attributeName, function )function

Type: Function( Integer index, String attr )

类型:函数(整数索引,字符串属性)

Hence, it expects two parameters. However, in our code, we need only the id, which happens to be the second parameter.

因此,它需要两个参数。然而,在我们的代码中,我们只需要id,它恰好是第二个参数。

Now, because of the way JS handles function arguments, we cannot write it as function(id), as JS would map idto index(the first argument expected for function). Thus, the function we write needs to have two parameters.

现在,由于 JS 处理函数参数的方式,我们不能将它写为function(id),因为 JS 会映射idindex(预期的第一个参数function)。因此,我们编写的函数需要有两个参数。

Here, a standard convention comes into play. As mentioned here,

在这里,一个标准约定开始发挥作用。正如这里提到的,

The underscore character (_) is used as a standard way to indicate an unused function argument.

下划线字符 (_) 用作指示未使用的函数参数的标准方式。

However, this is only a convention and not a rule. We could name the unused argument as indexor unusedjust as well. That is,

但是,这只是惯例,而不是规则。我们可以将未使用的参数命名为 asindexunusedas well。那是,

$(this).val('').attr('id', function(unused, id) { return id + i });

would be a valid equivalent.

将是一个有效的等价物。

Thus, such a usage of _ to substitute for an unused argument can be used for any other jQuery function that has a similar overridden form. For example, in thisanswer, we can see the usage of underscore in the call to $.text(). Just to confirm, $.text()has an overridden form that accepts a function with two arguments, as shown here.

因此,这种使用 _ 代替未使用参数的用法可用于任何其他具有类似覆盖形式的 jQuery 函数。例如,在这个答案中,我们可以看到在调用中下划线的用法$.text()。只是为了确认,$.text()有接受函数有两个参数,如图重写的形式在这里

回答by UltraInstinct

By style, _is typically used as a placeholder variable. A variable which wont be really used in the scope.

按样式,_通常用作占位符变量。一个不会在作用域中真正使用的变量。

回答by venimus

Although all answers explain that this is a code style "hack" to indicate an unused parameterit is generally a bad practice(idea). It will override any variable _declared in a parent scope. This will prevent you to use libraries that define _(as underscorejs for instance). It is not even some kind of an optimization as it declares a variable anyways.

尽管所有答案都解释说这是一种代码风格的“hack”,以指示未使用的参数,但它通常是一种不好的做法(想法)。它将覆盖_在父作用域中声明的任何变量。这将阻止您使用定义的库_(例如 underscorejs)。它甚至不是某种优化,因为它无论如何都声明了一个变量。

You should better use a descriptive name of the parameter and consider _just as a regular variable. Shortening variable names are also considered bad practice. So, if you plan to use this "clever hack", please don't.

您最好使用参数的描述性名称并将其视为_常规变量。缩短变量名称也被认为是不好的做法。所以,如果你打算使用这个“聪明的黑客”,请不要。

回答by IonicBurger

As all the other posters before have said, it can be seen as a placeholder, in cases where it is not expected to be used. I have seen something similar in python, when a function can return multiple values, but only the required ones are explicitly defined.

正如之前所有其他海报所说,它可以被看作是一个占位符,在不希望使用它的情况下。我在 python 中看到过类似的东西,当一个函数可以返回多个值时,但只显式定义了所需的值。

For example

例如

 a, _, c = foo(bar)

In this case the potential return value of 'b' is being ignored (which could still be accessed by _), and 'a' and 'c' are going to be used.

在这种情况下,'b' 的潜在返回值将被忽略(它仍然可以被 _ 访问),并且将使用 'a' 和 'c'。