[removed] what does function(_) mean

后端 未结 3 628
别那么骄傲
别那么骄傲 2020-12-16 19:44

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 alw         


        
相关标签:
3条回答
  • 2020-12-16 19:59

    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.

    0 讨论(0)
  • 2020-12-16 20:06

    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 ().

    0 讨论(0)
  • 2020-12-16 20:11

    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.

    0 讨论(0)
提交回复
热议问题