简体   繁体   English

骨干错误来自何处?

[英]Where is the Backbone error coming from?

So, I am creating a basic view using Backbone, and when I go to run it, I get this TypeError: 因此,我正在使用Backbone创建一个基本视图,当我运行它时,出现此TypeError:

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

The error is referencing multiple points inside the backbone.min.js file and line 18 in my program. 错误是在我的程序中引用了backbone.min.js文件和第18行中的多个点。 Here is the code: 这是代码:

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

I have been looking all over the internet and some people have encountered the same error, but it isn't related to Backbone. 我一直在互联网上查找,有些人也遇到了相同的错误,但这与Backbone无关。 Is the solution for Backbone the same as the others? Backbone的解决方案与其他解决方案一样吗? How can I fix this? 我怎样才能解决这个问题?

  1. Use the latest version of Backbone which at the moment is 1.3.3. 使用当前版本为1.3.3 的Backbone最新版本
  2. Use the non-minified version of any lib while in development. 在开发过程中,请使用任何lib的非压缩版本 There's even a big Development Version button to download the Backbone source file. 甚至还有一个很大的“ 开发版本”按钮可以下载Backbone源文件。
  3. Read the dependencies list carefully, which for version 1.0.0 of Backbone is: 请仔细阅读依赖项列表,对于1.b版本的Backbone ,它是:

    Backbone's only hard dependency is Underscore.js ( >= 1.4.3). Backbone唯一的硬依赖性是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 . 为了实现RESTful持久性,可通过Backbone.Router支持历史记录,并使用Backbone.View进行DOM操作,包括json2.js以及jQuery (> = 1.7.0)或Zepto

    And for version 1.3.3 : 对于1.3.3版本

    Backbone's only hard dependency is Underscore.js ( >= 1.8.3). Backbone唯一的硬依赖性是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. 为了使用Backbone.View实现 RESTful持久性和DOM操作,请包括jQuery (> = 1.11.0)和json2.js,以支持较早的Internet Explorer。 (Mimics of the Underscore and jQuery APIs, such as Lodash and Zepto , will also tend to work, with varying degrees of compatibility.) (类似Underscore和jQuery API的LodashZepto等也可以在不同程度的兼容性下正常工作。)

So, in the code you shared, it looks like jQuery is missing. 因此,在您共享的代码中,jQuery似乎丢失了。 Just include it before the other libs and you should be ready to use Backbone. 只需在其他库之前包含它,您就可以使用Backbone了。

Also, note that you're using the el option of the view with #paragraph element which doesn't exist yet in the document when executing that script snippet. 另外,请注意,您正在使用带有#paragraph元素的视图的el选项 ,该元素在执行该脚本代码段#paragraph不存在于文档中。 To fix this, place the script tag at the bottom of the body. 要解决此问题,请将script标签放置在正文的底部。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM