Backbone.js: `extend` undefined?

Javascriptbackbone.js

Javascript Problem Overview


Just getting started with Backbone.js. Simply including Backbone (either dev/production versions) causes the error:

Uncaught TypeError: Cannot call method 'extend' of undefined on Line 128:

// Attach all inheritable methods to the Model prototype
_.extend(Backbone.Model.prototype, Backbone.Events, 

Javascript Solutions


Solution 1 - Javascript

The issue was that I wasn't loading underscore.js. I totally missed that dependency in the docs. Duh.

Further clarification from @tjorriemorrie: I had underscore, but loaded in the wrong order, first load underscore (guess that is what 'dependency' means :)


Further Clarification just in case this isn't obvious. The order that things are loaded in JavaScript relates to the order the show up on the page. To load underscore first, be sure that the script tag including it comes before the one loading backbone. Like this:

<script src="underscore-1.4.4-min.js"></script>
<script src="backbone-1.0.0-min.js"></script>

Solution 2 - Javascript

Backbone only hard dependency is Underscore.js load underscorejs script before backbonejs script

Solution 3 - Javascript

The order is also important. I got the same error and it was was not resolved until I gave the underscore.js before backbone.js.

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js" type="text/javascript"></script>
  <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js" type="text/javascript"></script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt DarbyView Question on Stackoverflow
Solution 1 - JavascriptMatt DarbyView Answer on Stackoverflow
Solution 2 - JavascriptSomnath KokaneView Answer on Stackoverflow
Solution 3 - JavascriptHaris NpView Answer on Stackoverflow