new Backbone.Model() vs Backbone.Model.extend()

前端 未结 2 1172
太阳男子
太阳男子 2020-12-23 09:04

I\'m new to JS and Backbone

What\'s the difference between these two?

TestModel = new Backbone.Model({ title: \"test title\" })
TestModel = Backbone.         


        
2条回答
  •  清酒与你
    2020-12-23 10:00

    In the second case TestModel is a contructor which you can use several times later to create an instance of model:

    var model = new TestModel();
    

    However passing title to extend has a different meaning. You should probably use:

    var TestModel = Backbone.Model.extend({defaults: { title: "test title" }});
    

    or pass the model attributes when creating an object:

    var model = new TestModel({ title: "test title" });
    

    In the first case on the other hand TestModel is already an instance of model (hence it should be named testModel to follow JavaScript naming convention):

    var testModel = new Backbone.Model({ title: "test title" })
    

提交回复
热议问题