admin管理员组文章数量:1326345
I saw the following line in a node js code base.
_ => { return resolve(abc);}
Can any one give me some hint on what this _ means? I know => is fat arrow function. This line is from a snippet of promise.
I saw the following line in a node js code base.
_ => { return resolve(abc);}
Can any one give me some hint on what this _ means? I know => is fat arrow function. This line is from a snippet of promise.
Share Improve this question asked Jul 11, 2017 at 19:47 ZZZZZZ 3,78410 gold badges35 silver badges38 bronze badges 1- This looks like it is a repeat of stackoverflow./a/23903247/3517292 – ardev Commented Jul 11, 2017 at 21:31
4 Answers
Reset to default 5At the coaxing of a discussion elsewhere in this question, I'll turn my ments into an answer.
First off, I assume you aren't asking about arrow functions in general, but are just curious about the _
in your code sample.
There is a convention (used by some) to declare and name a parameter to a callback function that you know is passed, but you are not going to be using with an underscore as a sort of placeholder. It's a signal to people reading the code that there is an argument being passed to this callback, but you don't plan on using it in this particular invocation. It's presence is not functional, but more for
Now, without knowing what was in the head of the developer who wrote the line of code you asked about, we can't know for sure what the underscore means, but it does appear to match that previously described convention. There is one argument to the callback and it is not used in the implementation of the callback.
So, if our guess that this is a use of that convention is true, then in the arrow function that you show:
_ => { return resolve(abc);}
It is likely expecting one argument, but this particular use of that callback does not plan on using it, thus they named it with an _
just to signal that.
Now, there is no particular reason in Javascript that the callback needs to even define a single argument like this that be used. The callback could just as well have been defined as:
() => { return resolve(abc);}
since there's no requirement in Javascript that you declare arguments you aren't going to use.
If the callback took two arguments and it was the first one that you weren't going to use, then it does have to be declared as something in order to allow access to the second argument:
(_, secondArg) => { return resolve(secondArg);}
It means that the arrow function argument is not used. They use _
as argument name for the arrow function.
Doesn't this mean something like this?
function(_){
return resolve(abc);
}
What's happening here is that the code is using (some of) the concise syntax for arrow functions. To do this you'd write a function like this:
var f = x => x*x;
according to the docs
So with your example, the argument x is named with an _ (a valid character for variable names):
var f = _ => {return resolve(abc)}
But, it doesn't need the return or the brackets, either. It could be just:
_ => resolve(abc);
Perhaps a more readable way to write it would be with the parathesis:
() => resolve(abc);
本文标签: javascriptWhat does this underscore mean in node jsStack Overflow
版权声明:本文标题:javascript - What does this underscore mean in node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742198291a2431487.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论