检查Ember.js:获取对象的类型(Class)?

kra*_*er1 36 ember.js

我经常使用console.log(),特别是与...结合使用Ember.inspect().但是有一件事我想念:

如何找出对象的类型(Class)?

例如:<Sandbox.ApplicationController:ember288>在检查时得到类似的东西Ember.get("controller")

Ker*_*ick 74

如果您只想要型号名称(例如app/models/comment.js具有型号名称comment),则可以使用thing.constructor.modelName.

例如:

var aComment = this.get('store').createRecord('comment');
aComment.get('constructor.modelName') // => 'comment'
Run Code Online (Sandbox Code Playgroud)

  • 这个.这就是我需要的. (3认同)

Dav*_*gle 24

我知道你正在寻找一个字符串用于调试目的,但我最初想要知道如何获取对象的类型,而不是描述对象的字符串.

使用内置的Javascript属性构造函数将生成用于构造实例的类.例如,您可以这样做:

person = App.Person.create();
person.constructor // returns App.Person
person.constructor.toString() // return "App.Person"
Run Code Online (Sandbox Code Playgroud)


Luk*_*lia 18

如果你得到了Class,你通常可以调用toString()(或作为一个快捷方式连接一个空字符串+ '')来获得类似的东西<Sandbox.ApplicationController:ember288>