Backbone 错误来自哪里?

Where is the Backbone error coming from?

所以,我正在使用 Backbone 创建一个基本视图,当我转到 运行 它时,我得到了这个 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. 使用目前为 1.3.3 的 latest version of Backbone
  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.

另外,请注意,在执行该脚本片段时,您正在使用视图的 #paragraph 元素,该元素在文档中尚不存在。要解决此问题,请将脚本标记放在正文底部。

<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>