我有以下父对象:

Context = {
   ContextModel: Backbone.Model.extend({
      //model Code
   }),
   ContextList:Backbone.Collection.extend({
      model : Context.ContextModel
      // collection Code
   }),
   Contexts: new Context.ContextList,
   ContextView: Backbone.View.extend({
      // view Code
   })
}


在上面的代码中,model : Context.ContextModel引发错误,提示Uncaught ReferenceError: Context is not defined。我已经定义了上下文对象,但是以某种方式看不到它。有人可以帮我吗。
谢谢

最佳答案

让我们看一下JavaScript解释器的眼睛。您有一个声明,Context = { ... }。为了执行该语句,它必须首先构造{ ... },以便可以将其分配给Context。为了构造{ ... },需要评估new Context.ContextList。不幸的是,它仍在构建{ ... }部分,并且尚未为Context分配任何内容。因此,当您尝试创建Context的新实例时,Context.ContextList是未定义的。创建Context.ContextModel时,尝试访问Context.ContextList时遇到相同的问题。尝试这个:

Context = {
   ContextModel: Backbone.Model.extend({
      //model Code
   }),
   ContextView: Backbone.View.extend({
      // view Code
   })
}
Context.ContextList=Backbone.Collection.extend({
    model : Context.ContextModel
    // collection Code
});
Context.Contexts=new Context.ContextList();

09-07 16:20