Why include jQuery and underscore in every JS file in Backbone/Require.js project

backbone.js, javascript, jquery, requirejs, underscore.js

Solution

Declaring Underscore and jQuery as dependencies when you don't explicitly need them as variables in your module doesn't serve any purpose (i.e. it is not a best practice). As you said in your question

Personally, I would define jQuery or Underscore in a file that explicitly used their functions--but in something like a simple no-frills Backbone model file, they seem like cruft.

Moreover, you can even get rid of them in some situations:

- in views, use `this.$` to access the local DOM, `this.$el` for the element

- `Backbone.$` stores a reference to jQuery

- use the proxied methods of Underscore on models and collections

Problem

In almost every Backbone/Require.js project you will see models and views that look similar to this: ``` define([ 'jquery', 'underscore', 'backbone' ], function ($, _, Backbone) { //Some code goes here, perhaps a Backbone model or view }); ``` But, assuming that you set up your Require.js shims correctly (with the Backbone shim including something like `deps: ["underscore", "jquery"]`) you only need to define Backbone--defining Backbone as a dependency implicitly defines jQuery and Underscore as dependencies as well! Thus this would also be correct: ``` define([ 'backbone' ], function (Backbone) { //Some code goes here, perhaps a Backbone model or view }); ``` Personally, I would define jQuery or Underscore in a file that explicitly used their functions--but in something like a simple no-frills Backbone model file, they seem like cruft. Why do I so frequently see the pattern of superfluous jQuery and Underscore definitions? Why has this become an unquestioned best practice?

Original source