user2814599
user2814599

Reputation: 1180

Please, explain, what means when _ is passed to a function in js?

define([
'jquery',
'underscore',
'backbone',
'models/Evaluate'
], function ($, _, Backbone, Evaluate) {

var MyCollection = Backbone.Collection.extend({
    model:Evaluate,
    url:'evaluate/process'
});

return MyCollection;
});

or there are also cases, when _ is returned from a function

return _;

Upvotes: 1

Views: 77

Answers (4)

Yair Nevet
Yair Nevet

Reputation: 13003

JavaScript allows you to name your variables/functions with various characaters, such as: $, _

So given that, this probably another variable name.

Upvotes: 0

Lix
Lix

Reputation: 47966

This is the name of the variable that holds the underscore plugin.

In a similar manner, the $ character is used for jQuery.

Both of these can be changed, but these are the defaults that each library sets up.


In your example, you are using requireJS to load dependencies, these will be passed in the same order that they are listed. jQuery first and then underscore, this is why the function is passed:

function ($, _, ...)
     //   ---^ underscoreJS
     //   ^ jQuery

Hands on example:

Calling the uniq underscore function:

_.uniq([1, 2, 1, 3, 1, 4]);

Calling the each jQuery function:

$.each([ 52, 97 ], function(...) );

Upvotes: 0

matewka
matewka

Reputation: 10148

_ is the name of a variable. The same as $ is a variable commonly used as an alias for jQuery variable.

Upvotes: 0

CassOnMars
CassOnMars

Reputation: 6181

_ is a variable name. In this case, it is referring to the JavaScript library Underscore.

Upvotes: 2

Related Questions