"Uncaught TypeError:undefined不是函数"初始化Backbone集合

Dav*_*les 11 backbone.js

我有一个Backbone集合,如下所示:

var FooCollection = Backbone.Collection.extend({
    model:Foo,

    initialize: function (attributes, options) {
        this.barId = options.barId;
    }
});

var Foo = Backbone.Model.extend({});
Run Code Online (Sandbox Code Playgroud)

当我尝试初始化它时,我在_prepareModel()函数中得到"未捕获的TypeError:undefined不是函数" Backbone.Collection.

糟糕的电话是在model = new this.model(attrs, options).

// Prepare a model or hash of attributes to be added to this collection.
_prepareModel: function(model, options) {
  options || (options = {});
  if (!(model instanceof Model)) {
    var attrs = model;
    options.collection = this;
    model = new this.model(attrs, options); // <-- BLOWS UP HERE
    if (!model._validate(model.attributes, options)) model = false;
  } else if (!model.collection) {
    model.collection = this;
  }
  return model;
},
Run Code Online (Sandbox Code Playgroud)

当我步_prepareModel()在调试器,它看起来像的类型this在这一点是child,和this.model,事实上,不确定的.

谁能告诉我我做错了什么?

Dav*_*les 18

在我的实际代码Foo之后宣布FooCollection.没有意识到Javascript不支持前向声明.[headdesk]

  • 请记住,`var a = 6;`是两个一样的东西:`var a;`和`a = 6;`.`var`声明将被提升到相关范围的顶部,但分配不会. (5认同)