gpt4 book ai didi

javascript - Backbone 错误从何而来?

转载 作者:行者123 更新时间:2023-11-30 09:36:58 24 4
gpt4 key购买 nike

所以,我正在使用 Backbone 创建一个基本 View ,当我运行它时,我得到了这个 TypeError:

Uncaught TypeError: Right-hand side of instanceof is not an object

错误引用了 backbone.min.js 文件中的多个点和我程序中的第 18 行。这是代码:

<!DOCTYPE HTML>

<HTML>
<HEAD>
<META CHARSET="UTF-8" />
<TITLE>First View</TITLE>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.0.0/backbone-min.js"></script>
<SCRIPT LANGUAGE="Javascript">
var PokeView = Backbone.View.extend({
render: function() {
this.$el.html("Simisage");

return this;
}
});

var myPokeView = new PokeView({ el: "#paragraph" });

console.log(myPokeView instanceof Object);
myPokeView.render();
</SCRIPT>
</HEAD>
<BODY>
<p id="paragraph"></p>
</BODY>

我一直在网上寻找,有些人遇到了同样的错误,但它与 Backbone 无关。 Backbone 的解决方案是否与其他解决方案相同?我该如何解决这个问题?

最佳答案

  1. 使用latest version of Backbone目前是 1.3.3。
  2. 在开发过程中使用任何库的非缩小版本。甚至还有一个很大的开发版本按钮来下载 Backbone 源文件.
  3. 仔细阅读依赖列表,version 1.0.0 of Backbone是:

    Backbone's only hard dependency is Underscore.js ( >= 1.4.3). For RESTful persistence, history support via Backbone.Router and DOM manipulation with Backbone.View, include json2.js, and either jQuery ( >= 1.7.0) or Zepto.

    对于 version 1.3.3 :

    Backbone's only hard dependency is Underscore.js ( >= 1.8.3). For RESTful persistence and DOM manipulation with Backbone.View, include jQuery ( >= 1.11.0), and json2.js for older Internet Explorer support. (Mimics of the Underscore and jQuery APIs, such as Lodash and Zepto, will also tend to work, with varying degrees of compatibility.)

因此,在您共享的代码中,jQuery 似乎缺失了。只需将它包含在其他库之前,您就应该准备好使用 Backbone。

另外,请注意您使用的是 el option在执行该脚本片段时,带有 #paragraph 元素的 View 在文档中尚不存在。要解决此问题,请将脚本标记放在主体的底部。

<html>
<head>
<meta charset="UTF-8" />
<title>First View</title>
</head>
<body>
<p id="paragraph"></p>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone.js"></script>
<script>
var PokeView = Backbone.View.extend({
render: function() {
this.$el.html("Simisage");
return this;
}
});

var myPokeView = new PokeView({ el: "#paragraph" });

console.log(myPokeView instanceof Object);
myPokeView.render();
</script>
</body>
</html>

关于javascript - Backbone 错误从何而来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43018207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com