gpt4 book ai didi

javascript - Backbone.js 疑问

转载 作者:行者123 更新时间:2023-11-28 12:08:27 28 4
gpt4 key购买 nike

我有以下父对象:

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 Define。我已经定义了上下文对象,但不知何故它没有看到它。有人可以帮我吗?谢谢

最佳答案

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

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();

关于javascript - Backbone.js 疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6688014/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com