Javascript 未定义不是主干中的函数错误

Javascript 未定义不是主干中的函数错误,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,我有一个包含主干代码的html模板。我是主干的新手。每当我尝试运行代码时,它会显示未定义的错误不是函数错误。请帮助 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>hello-backbonejs</title> </head> <body>

我有一个包含主干代码的html模板。我是主干的新手。每当我尝试运行代码时,它会显示未定义的错误不是函数错误。请帮助

  <!DOCTYPE html>
      <html>
      <head>
        <meta charset="utf-8">
        <title>hello-backbonejs</title>
      </head>
      <body>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script src="http://ajax.cdnjs.com/ajax/libs/json2/20110223/json2.js"></script>
        <script src="http://ajax.cdnjs.com/ajax/libs/underscore.js/1.1.6/underscore-min.js"></script>
        <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>



        <script type="text/javascript">
        (function($){
        //ListView class: Our main app view.

      var ListView = Backbone.View.extend({
        el: $('body'), // attaches `this.el` to an existing element.

    //initialize(): Automatically called upon instantiation. Where you make all types of bindings, excluding UI events, such as clicks, etc.

        initialize: function(){
          _.bindAll(this, 'render'); // fixes loss of context for 'this' within methods

           this.render(); // not all views are self-rendering. This one is.
        },

    //render(): Function in charge of rendering the entire view in this.el. Needs to be manually called by the user.

        render: function(){
          $(this.el).append("<ul> <li>hello world</li> </ul>");
        }
      });

    //listView instance: Instantiate main app view.

      var listView = new ListView();
        })(jQuery);
        </script>
      </body>
      </html>

你好,backbonejs
(函数($){
//ListView类:我们的主应用程序视图。
var ListView=Backbone.View.extend({
el:$('body'),//将'this.el'附加到现有元素。
//initialize():在实例化时自动调用。您可以在其中进行所有类型的绑定,不包括UI事件,如单击等。
初始化:函数(){
_.bindAll(this'render');//修复了方法中“this”上下文的丢失
this.render();//并非所有视图都是自呈现的。这一个是。
},
//render():用户需要手动调用负责在this.el.中呈现整个视图的函数。
render:function(){
$(this.el)。追加(“
  • 你好,世界
    • ”; } }); //listView实例:实例化主应用程序视图。 var listView=new listView(); })(jQuery);
该问题是由下划线.js的旧版本引起的。将其更改为较新版本后,错误消失,代码正常工作

演示中使用的下划线版本:

//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js

另外,如果您查看JSFIDLE左侧accordion菜单中的外部资源,您可以看到导入的文件(在HEAD中)。

错误消息应该给您一个行号,这样您就可以知道哪个变量是未定义的。告诉我们错误发生的行将极大地帮助我们。