Javascript Backbone.js:`extend` 未定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4875743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:33:53  来源:igfitidea点击:

Backbone.js: `extend` undefined?

javascriptbackbone.js

提问by Matt Darby

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

刚刚开始使用 Backbone.js。简单地包含 Backbone(开发/生产版本)会导致错误:

Uncaught TypeError: Cannot call method 'extend' of undefinedon Line 128:

Uncaught TypeError: Cannot call method 'extend' of undefined在第 128 行:

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

回答by Matt Darby

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

问题是我没有加载underscore.js. 我完全错过了文档中的依赖项。呃。

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

@tjorriemorrie 的进一步澄清:我有下划线,但加载顺序错误,首先加载下划线(猜猜这就是“依赖”的意思:)



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:

进一步澄清以防万一这不明显。JavaScript 中加载的顺序与页面上显示的顺序有关。要首先加载下划线,请确保包含它的脚本标记位于加载主干之前。像这样:

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

回答by Somnath Kokane

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

Backbone 唯一的硬依赖是 Underscore.js 在 Backbonejs 脚本之前加载 underscorejs 脚本

回答by Haris Np

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.

顺序也很重要。我遇到了同样的错误,直到我在backbone.js 之前给出underscore.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>