获取模型类型的传递Backbone.js的模型实例 [英] Get the model type for a passed in backbone.js model instance

查看:91
本文介绍了获取模型类型的传递Backbone.js的模型实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何为Backbone.js的模型实例检索模型名称?

How do I retrieve the model name for a backbone.js model instance?

例如:

var Topic = Backbone.Model.extend({

})
var topic = new Topic({ type: 'question' })

var doSomethingWithTopic = function(topic) {
  // check if passed in topic is of type Topic
  // something like topic.constructor.name === 'Topic'
}

doSomethingWithTopic(topic)

我意识到我可能是一个模糊Backbone.js的模型和类之间的界限,所以我打开的如果需要的话要对这个其他方式。

I realize I may be blurring the line between a backbone.js model and a class, so I am open to other ways of going about this if needed.

推荐答案

使用的instanceof 运营商。

var doSomethingWithTopic = function(topic) {
  if( topic instanceof Topic ) {
    // do something with topic
  }
}

这篇关于获取模型类型的传递Backbone.js的模型实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆