24

I came across the following javascript code:

this.removeEdge = function(source, target) {
  if(!_states[source]) return;

  var children = _states[source].children,
      index = _(children).indexOf(target);
  if(index !== -1) children.splice(index, 1);
};

What does _(children) mean?

Charles Gao
  • 599
  • 2
  • 5
  • 13
  • Might find your answer here: http://stackoverflow.com/questions/4484424/underscore-prefix-for-property-and-method-names-in-javascript – showdev Apr 16 '13 at 20:34
  • 4
    The `_` is a JavaScript identifier, probably for the [underscore](http://underscorejs.org/) library in this case. – Rob W Apr 16 '13 at 20:35
  • 1
    @showdev: Actually `_()` is a call to a function called.. well.. `_` – Antoine Lassauzay Apr 16 '13 at 20:36
  • @AntoineLassauzay A JavaScript identifier is either a letter, `$`, or `_`. By putting `_` on the stack as a function type doesn't mean it is not an identifier. – Ryan McCullagh Aug 13 '14 at 00:42

3 Answers3

29

_ is a valid variable identifier in JavaScript, and could theoretically refer to anything. Using _(...) with function syntax implies that _ is a function.

That said, it is commonly used by the underscore.js library, however if you're looking at minified code, it's quite possibly being used as another single-character variable name to save on file size.


In your example provided, it appears that underscore.js is being used to treat children as a collection, so that the indexOf function can be applied to the collection. This would be similar to calling:

_.indexOf(children, target);
zzzzBov
  • 157,699
  • 47
  • 307
  • 349
  • I'm not looking at minified code. If _ is a function that passes children as its parameter, I don't get its meaning because there is no function definition for _. – Charles Gao Apr 16 '13 at 20:41
  • @CharlesGao, the minified code remark was meant as a general comment, not directed to your specific situation. This case looks as though the code's using the underscore library for its utility functions which iterate over collections. – zzzzBov Apr 16 '13 at 20:44
11

Came looking for an answer to this and managed to find one. The _(variable) statement wraps underscore around the variable. According to this link in the "Object-Oriented and Functional Styles" section,

index = _(children).indexOf(target);

is equivalent to

index = _.indexOf(children, target);

The first is written in object-oriented style, which allows chaining of functions. Their example is as follows:

_(lyrics).chain()
  .map(function(line) { return line.words.split(' '); })
  .flatten()
  .reduce({}, function(counts, word) { 
    counts[word] = (counts[word] || 0) + 1;

Each of these functions returns the underscore function wrapping lyrics, allowing chained manipulation of the lyrics variable.

Underscore changelog:

0.4.0 — November 7, 2009: All Underscore functions can now be called in an object-oriented style, like so: _([1, 2, 3]).map(...);. Original patch provided by Marc-André Cournoyer. Wrapped objects can be chained through multiple method invocations. A functions method was added, providing a sorted list of all the functions in Underscore.

jahmezz
  • 396
  • 1
  • 3
  • 13
  • [Documentation for _(.) for _lodash_ 4.17.4](https://lodash.com/docs/4.17.4#lodash), which apparently merges _lodash_ and _underscore_, – David Tonhofer Nov 09 '17 at 13:19
  • And a nice explanation about "implicit chaining" (as opposed to "explicit chaining"): [Implicit Function Chaining in Lodash](https://blog.mariusschulz.com/2015/05/14/implicit-function-chains-in-lodash). Dat Syntactic Sugar! – David Tonhofer Nov 09 '17 at 18:17
2
class Book {
  constructor(author) {
    this._author = author;
}

It is convention to precede the name of a private variable with an underscore (_). However, the practice itself does not make a variable private.