首页 > 解决方案 > 未定义变量-backbone.js

问题描述

我对backbone.js很陌生,我正在尝试从文件B访问一个var到文件A。目前我已经声明了文件A,如下所示

window.dog = Backbone.Collection.extend({

    model : animal,

    initialize : function(){
        _.bindAll(this);
    },
  // this is where i am getting an error saying i have not defined dog
  var dog=  new dog();
})

文件 B:

var  dog = function () {
    this.eats = {};
    this.dance.REQUEST = {};
    this.walk.REQUEST.LIST = [];

};

现在我希望从脚本 A 调用方法 eat 我该怎么做?

标签: javascriptbackbone.jsbackbone.js-collections

解决方案


这是一个语法错误。您不能只在对象内定义变量。

window.dog = Backbone.Collection.extend({
	model : animal,
	initialize : function(){
	 _.bindAll(this);
	},
    // change to this 
    dog :  new dog()
});


推荐阅读