31

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?

4
  • Might find your answer here: stackoverflow.com/questions/4484424/… Commented Apr 16, 2013 at 20:34
  • 4
    The _ is a JavaScript identifier, probably for the underscore library in this case. Commented Apr 16, 2013 at 20:35
  • 1
    @showdev: Actually _() is a call to a function called.. well.. _ Commented Apr 16, 2013 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. Commented Aug 13, 2014 at 0:42

4 Answers 4

44

_ 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); 
Sign up to request clarification or add additional context in comments.

2 Comments

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 _.
@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.
13

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.

2 Comments

Documentation for (.) for _lodash 4.17.4, which apparently merges lodash and underscore,
And a nice explanation about "implicit chaining" (as opposed to "explicit chaining"): Implicit Function Chaining in Lodash. Dat Syntactic Sugar!
3
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.

Comments

0

in javascript _ in front of the name of the property means that that property should not be altered so that why we use underscore so that we can tell that property should not altered.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.