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
}
In 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With