Javascript: what does function(_) mean
Asked Answered
B

3

13

I'm going through the bacon.js slide at: http://raimohanska.github.io/bacon.js-slides/1.html

In the 1st line of the 2nd block, it says:

function always(value) { return function(_) { return value } }

what does function(_) mean?

Busoni answered 28/5, 2014 at 5:6 Comment(3)
_ is simply a parameter in this context.Staats
It’s not meaningful here, in JavaScript, but the convention is that _ represents an ignored value.Barrie
@false Ironically "ignore this" did not work, and brought OP attention. I also have never heard about this convention. Interesting.Beeswax
K
25

In this case _ is just a function parameter - a single underscore is a convention used by some programmers to indicate "ignore this binding/parameter".

Since JavaScript doesn't do parameter-count checking the parameter could have been omitted entirely. Such a "throw-away" identifier is found more commonly in other languages, but consider a case like arr.forEach(function (_, i) {..}) where _ indicates the first parameter is not to be used.

Kimon answered 28/5, 2014 at 5:12 Comment(6)
That makes at least some sense of it. Thanks for sharing.Bagatelle
F# (and similar) do this consistently during pattern matching. C# can also do this for lambda variables. Its definitely common.. however useless it may be in the code you are reviewing.Odericus
I used this convention along with the Haskell type signatures in the slides. I prefer typesafe languages and tend to use some Haskell conventions in Javascript code too. Sorry for causing confusion!Phytogeography
but if it is not going to be used, why not just say function (i)={...}? is not the same?Marlborough
@Marlborough It's a convention. The parameter might as as well be called unused or thisParameterIsNotUsedUsed.Kimon
yes I understand that it a convention for saying we are not using it, I just did not understand that sometimes we only need the iterator or index value (key) and in that case the callback function will require 2 parameters, the current value, and the index.Marlborough
E
13

It's an anonymous function with one argument, the name of that argument is _.

I don't know why they bother with the argument, since the function doesn't use it.

Emergence answered 28/5, 2014 at 5:9 Comment(3)
Upvoted to reverse the downvote, as I'm pretty sure that this answer is correct. The underscore becomes the name of the first argument. It is not used.Gilberto
"Upvoted to reverse the downvote" - that's funny, but you're right there are many nervous people around :)Bagatelle
+1 for I don't know why they bother with the argument, since the function doesn't use it.Baer
B
1

It's the same as putting any other identifier to a list of arguments according to this document: http://mathiasbynens.be/notes/javascript-identifiers

You'll find in this doc that _ is a legal character that an identifier can start with.

There is no any meaning for this in your example, probably the author just thought that it's cooler than just ().

Bagatelle answered 28/5, 2014 at 5:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.