I am learning backbone now. And I have a problem like this: can I get type
backbone model
. Backbone have a function like typeof
in javascript or instanceof
in java. Like that:
getModelTypeot: function(model) {
// return model type
}
I am learning backbone now. And I have a problem like this: can I get type
backbone model
. Backbone have a function like typeof
in javascript or instanceof
in java. Like that:
getModelTypeot: function(model) {
// return model type
}
Share
Improve this question
edited Mar 7, 2013 at 12:38
Ulug'bek
asked Mar 7, 2013 at 12:30
Ulug'bekUlug'bek
2,8326 gold badges34 silver badges62 bronze badges
1 Answer
Reset to default 7In JavaScript each object has a reference to its constructor (a function that was used to create the object). It's accessible as obj.constructor
.
If you have a Backbone.js model, which is extended from Backbone.Model in this way: var YourModel = Backbone.Model.extend({});
, you could create an object using new
: var yourModel = new YourModel();
.
Then, you could use yourModel.constructor
:
yourModel.constructor === YourModel // true
Or instanceof
:
yourModel instanceof YourModel // true
yourModel instanceof Backbone.Model // true