jQuery 如何修复未捕获的类型错误:无法读取未定义的属性“原型”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19702782/
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
How to fix Uncaught TypeError: Cannot read property 'prototype' of undefined?
提问by Oleksandr H
I have a little problem. I'm using backbone.js. I wrote this code like in example:
我有一个小问题。我正在使用backbone.js。我在示例中编写了此代码:
<script>
$(document).ready(function () {
window.App = {
Views: {},
Models: {},
Collections: {}
}
App.Collections.Users = Backbone.Collection.extend({
model: App.Models.User,
url: 'service'
});
App.Models.User = Backbone.Model.extend({});
App.Views.App = Backbone.View.extend({
initialize: function() {
console.log( this.collection.toJSON() );
}
});
});
</script>
Than I started server and in browser console type this:
比我启动服务器并在浏览器控制台中输入:
var x =new App.Collections.Users();
x.fetch()
And this follows to error: Uncaught TypeError: Cannot read property 'prototype' of undefined
. But data is present in response. Details in picture. How to fix this?
Thanks for you answers.
这会导致错误:Uncaught TypeError: Cannot read property 'prototype' of undefined
. 但是数据是作为响应存在的。详情见图片。如何解决这个问题?谢谢你的回答。
回答by Oleksandr H
I fixed this bug. The problem was that I created Collection and then the Model. Collections use user model, as working unit, but when I defined this Collection, I did not define Model.
我修复了这个错误。问题是我创建了集合,然后是模型。集合使用用户模型作为工作单元,但是当我定义这个集合时,我并没有定义模型。
So, if you want to avoid this bug, firstly define a Model and only then define the Collection.
所以,如果你想避免这个bug,首先定义一个模型,然后再定义集合。
回答by rzskhr
Backbone Js has dependency with underscore.js and jQuery, try reordering the resources. This worked for me:
Backbone Js 依赖 underscore.js 和 jQuery,尝试重新排序资源。这对我有用:
<head>
<title>Backbone</title>
<!-- Scripts Load Here-->
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="js/underscore.js"></script>
<script type="text/javascript" src="js/backbone.js"></script>
</head>