I’ve been working in backbone.js and came across the following snippet of code.
_(view.buttonViews).each(function(button) {
button.render();
});
Where view.buttonViews is an array. If I take away the _() and have
view.buttonViews.each(function(button) {
button.render();
});
then I get an error that each is not a function. What does the _() add? Thanks!
I guess it is the
Underscore.jslibrary which provides theeachmethod:This way,
_([...]).each(...), is just another way of calling it.BTW, it is also described in Backbone’s documentation:
And FWIW, as @Jonathon already said, in general,
_is a valid variable name and in this case it contains a function. Adding parenthesis behind a function references calls that function and therefore,_()calls the function referred to by_. It is nothing special.Besides that, parenthesis can occur as part of a function declaration or expression (
function foo() {...}) or as grouping operator (var i = (20 + 1) * 2;).