获取主干模型实例的模型/类名称

2022-01-24 00:00:00 javascript backbone.js coffeescript

给定一个Backbone模型的实例,我怎么知道这个实例的类"(可以这么说)?

Given an instance of Backbone model, how can I know the "class" (so to speak) of this instance ?

例如:

class Car extends Backbone.Model

mycar = new Car()

而我需要的是:

mycar.modelName # => 'Car'

推荐答案

我认为总体上是有问题的.我打算建议这里提到的一些选项( 如何在 JavaScript 中获取对象类型的名称? ) 但我遇到了第一个选项的问题.您总是可以扩展您的所有模型,使其具有一个返回模型名称"的函数,但我可以理解为什么您会觉得这不太令人满意.

It's problematic in general, I think. I was going to suggest some of the options mentioned here ( How do I get the name of an object's type in JavaScript? ) but I was having issues with the first option. You could always extend all your models to have a function that returned a "name" for the model, but I can understand why you might find that less than satisfactory.

相关文章