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?
Variable means anything that can vary. In JavaScript, a variable stores the data value that can be changed later on. Use the reserved keyword var to declare a variable in JavaScript.
Updated on July 03, 2019. The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.
A single leading underscore in front of a variable, a function, or a method name means that these objects are used internally. This is more of a syntax hint to the programmer and is not enforced by the Python interpreter which means that these objects can still be accessed in one way on another from another script.
Double underscore (__) in front of a variable is a convention. It is used for global variable (The following variables may appear to be global but are not, rather local to each module) in Nodejs meanwhile Underscore(_) used to define private variable.
_
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With