我的 Backbone.View 函数未定义

my Backbone.View functions are undefined

我正在扩展 Backbone.View 然后调用它的渲染函数,但我得到一个 "Uncaught TypeError: view.render is not a function"

代码(运行 in document onload):

var view = Backbone.View.extend({
 id: "my-view",
 render: function() {
   console.log("in render, this =", this);
   this.el.innerHTML = "Hello";
   return this;
 }
});
console.log("outside view, view =", view);
document.body.appendChild(view.render().el);

控制台结果:

outside view, view = function (){return r.apply(this,arguments)}
VM639:58 Uncaught TypeError: view.render is not a function

看起来它应该是一个构造函数,但我尝试创建一个新的 view() 然后调用它的函数并得到一个不同的错误:

Uncaught TypeError: this._ensureElement is not a function
at e.View (backbone-min.js:1)   

我在别处寻找过这个问题的答案 (Backbone View: function is undefined, Render a View of Backbone Model returns undefined),但示例很复杂,我不知道如何将答案应用到我的代码中。我做了一个非常简单的例子,希望能鼓励你回答它,即使我的错误是非常基本的。

这是一个 jsfiddle:https://jsfiddle.net/s9Lhq82a/

您定义了一个从 Backbone.View 扩展的视图 class。要创建 class 的实例,您必须使用 new.

var View = Backbone.View.extend({
    // this is your View Class
});

var myView = new View(); // this is an instance of View, it has a render function
myView.render().el;