Javascript 主干中对象与函数的实例化,js

Javascript 主干中对象与函数的实例化,js,javascript,backbone.js,Javascript,Backbone.js,我接手了一个研究项目,一直做得很好,除了最近,我想我误解了在主干网中实现的一些JS结构。对于模型或集合的结构,以及它是返回对象还是返回函数,我感到困惑 该项目具有以下模型的当前结构: define([…], function(…) { var measureModel = Backbone.Model.extend({ defaults: {…}, initialize: function(){…} … }); return measureModel; //h

我接手了一个研究项目,一直做得很好,除了最近,我想我误解了在主干网中实现的一些JS结构。对于模型或集合的结构,以及它是返回对象还是返回函数,我感到困惑

该项目具有以下模型的当前结构:

define([…], function(…) {
  var measureModel = Backbone.Model.extend({
    defaults: {…},
    initialize: function(){…}
    …
  });
  return measureModel; //here there is no function, so I assume its an object
});
对于集合:

define([…], function(…){
  var measuresCollection = Backbone.Collection.extend({
    model: measureModel,
    initialize: function(){…}
  });
  return new measuresCollection(); //here I assume it is an function
});
我制作了具有上述结构的新模型和集合,但出现以下错误,因此我也尝试了以下方法:

define([…], function(…){
  return Backbone.Collection.extend({  // here I just return the Object
    model: newerModel,
    initialize: function(){…}
  });
});
按照第一个结构,在一些新模型和集合上,我得到了错误
uncaughttypeerror:object不是函数
uncaughttypeerror:[object object]不是函数
uncaughttypeerror:undefined不是函数
,这取决于是否省略了结束返回语句,或者直接返回对象

我在另一个视图中调用构造函数,如下所示:
this.newerCollection=newnewercollection()

  • 为什么我使用相同的结构得到(任何)这些错误
  • 创建变量、返回变量和直接返回变量之间有什么区别
  • 为什么我要用一种方法而不是另一种

extend
始终返回一个函数,该函数用作已扩展的模型/集合/视图的构造函数

在这个代码块(问题的第一个)中,您将返回一个函数,它是扩展模型的构造函数:

define([…], function(…) {
  var measureModel = Backbone.Model.extend({
    defaults: {…},
    initialize: function(){…}
    …
  });
  return measureModel; //here you are returning a function, which is a constructor for the extended Model
});
在这个代码块(问题的第二个)中,您返回的是一个对象,而不是一个函数,因为您已经使用
new
实例化了
measurecollection
measureCollection
变量本身就是构造函数:

define([…], function(…){
  var measuresCollection = Backbone.Collection.extend({
    model: measureModel,
    initialize: function(){…}
  });
  return new measuresCollection(); //here you are returning an object because it is instantiated using `new`
});
如果您尝试使用该模块的值来实例化一个新对象,您将得到“对象不是函数”错误

在这个代码块中(问题中的第三个块)相当于第二个块中的
return
ing
measuresCollection
。在这里,返回的是函数而不是对象,与第一个块的返回方式相同

define([…], function(…){
  return Backbone.Collection.extend({  // this is returning a function, the same as the first code block
    model: newerModel,
    initialize: function(){…}
  });
});
如果省略模块中的
return
语句,它将返回
undefined
,当您尝试使用它实例化对象时,将出现“undefined is not a function”错误


在第1个和第3个代码块中返回构造函数的方式基本上没有区别。前者只是在返回局部变量之前将构造函数赋值给它。执行此操作的唯一原因是,如果在返回它之前需要对其进行操作。

extend
始终返回一个函数,该函数用作已扩展的模型/集合/视图的构造函数

在这个代码块(问题的第一个)中,您将返回一个函数,它是扩展模型的构造函数:

define([…], function(…) {
  var measureModel = Backbone.Model.extend({
    defaults: {…},
    initialize: function(){…}
    …
  });
  return measureModel; //here you are returning a function, which is a constructor for the extended Model
});
在这个代码块(问题的第二个)中,您返回的是一个对象,而不是一个函数,因为您已经使用
new
实例化了
measurecollection
measureCollection
变量本身就是构造函数:

define([…], function(…){
  var measuresCollection = Backbone.Collection.extend({
    model: measureModel,
    initialize: function(){…}
  });
  return new measuresCollection(); //here you are returning an object because it is instantiated using `new`
});
如果您尝试使用该模块的值来实例化一个新对象,您将得到“对象不是函数”错误

在这个代码块中(问题中的第三个块)相当于第二个块中的
return
ing
measuresCollection
。在这里,返回的是函数而不是对象,与第一个块的返回方式相同

define([…], function(…){
  return Backbone.Collection.extend({  // this is returning a function, the same as the first code block
    model: newerModel,
    initialize: function(){…}
  });
});
如果省略模块中的
return
语句,它将返回
undefined
,当您尝试使用它实例化对象时,将出现“undefined is not a function”错误


在第1个和第3个代码块中返回构造函数的方式基本上没有区别。前者只是在返回局部变量之前将构造函数赋值给它。这样做的唯一原因是,如果您需要在返回它之前对其进行操作。

您如何使用第一个结构创建新模型?以及
this.newerCollection=new newerCollection()应该是
this.newerCollection=newerCollection
如果返回
new measuresCollection()
var componentView=new componentView({…})你能发布你正在试用的全部代码吗?-赫特拉克分行。具体地说,我正试图在页面上创建一个附加部分(systemLabelView.js),该部分由可拖动div的集合(labelcollection.js)组成,这些div具有自己的视图(SystemLabelButtonView.js),并绑定到一个模型(label.js).如何使用第一个结构创建新模型?以及
this.newerCollection=new newerCollection()应该是
this.newerCollection=newerCollection
如果返回
new measuresCollection()
var componentView=new componentView({…})你能发布你正在试用的全部代码吗?-赫特拉克分行。具体地说,我正试图在页面上创建一个附加部分(systemLabelView.js),该部分由可拖动div的集合(labelcollection.js)组成,这些div具有自己的视图(SystemLabelButtonView.js),并绑定到一个模型(label.js)那么,在选择返回一个对象还是返回一个函数之间,应该如何引导我呢?返回一个对象会将模块变成一个单例。第一次请求模块时,将运行该模块并返回该对象。使用内存中对象的后续时间。因此,如果您想实例化多个实例,请返回函数。那么,在选择返回对象与返回函数之间,我应该做些什么呢?返回对象会将模块变成一个单例。第一次请求模块时,将运行该模块并返回该对象。使用内存中对象的后续时间。因此,如果要实例化多个实例,请返回functi