最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get type backbone model? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

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
发布评论

评论列表(0)

  1. 暂无评论