문제

I'm new to Backbone.js as well as to JavaScript in general. while playing around a bit, I'll get to error message shown in questions title having these model definitions:

var Incredient = Backbone.Model.extend({
  initialize : console.log("A new Incredient object called " + name + "has been created."),
  defaults : {
    name : '',
    quantity_unit: '',
    need_to_buy : true
  }
});

var Dish = Backbone.Model.extend({
  initialize : console.log("A new Dish object with title " + title + "has been created."),
  defaults : {
    title : '',
    incredients : [],
    instructions : ''
  }
});

Strange thing is, this error appears in line 11, which holds

initialize : console.log("A new Dish object with title " + title + "has been created."),

the variable, which is not defined is 'title'. However, line 2 does not cause any error message in Chrome's JS console. Obviously, there's no such reference error for 'name' despite the otherwise similar declarations. What's the matter with line 11? Any help is much appreciated.

Thanks.

도움이 되었습니까?

해결책

try this:

var Dish = Backbone.Model.extend({
  defaults : {
    title : '',
    incredients : [],
    instructions : ''
  },
  initialize : function(){
      console.log("A new Dish object with title " + this.get('title') + "has been created.")
  }
});
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top