angular - 获取类的属性类型
问题描述
我想获取对象类的所有属性类型
export class Test extends BaseModel
{
id: number;
zone: CheckZone;
employee: Employee;
constructor() {
super();
this.id = 0;
this.zone = null;
this.employee = null;
}
}
// Printing the object properties and types
const object = new Test();
object.zone = new CheckZone();
for (const property in object) {
if (object.hasOwnProperty(property)) {
let propertyType = typeof object[property];
if (typeof object[property] === 'object') {
if (object[property] && object[property].constructor){
propertyType = object[property].constructor;
} else {
propertyType = null;
}
}
console.log('Name: ', property, 'Type: ', propertyType, 'Value: ', object[property]);
}
}
/*
* Console logs results
*
Obtained
Name: id Type: number Value: 0
Name: zone Type: CheckZone() {...} Value: CheckZone {...}
Name: employee Type: null Value: null
Required
Name: id Type: number Value: 0
Name: zone Type: CheckZone() {...} Value: CheckZone {...}
Name: employee Type: Employee ** HERE ** Value: null
Cannot get Employee because value is null
*/
我想获取定义为类型的属性的类名,即使它是 null 类型:对象 --> 类型:CheckZone 类型:对象 --> 类型:员工
解决方案
推荐阅读
- html - iOS OTC 自动完成 CSS 问题
- python - 在 try / except 中搜索数组
- arrays - 如何在 Elixir 中使用类似的方法填充列表,如其他语言中的 for 循环
- swiftui - 如何使用 SwipeAction 动画删除列表中的项目?领域,Swiftui
- sql - SQL INSERT INTO 多行
- arrays - 'float' 之前的预期表达式
- delphi - 如何提示用户确认程序退出?
- javascript - 获取具有指定优先级的人数(Discord.js)
- django - wagtail:在创建时将页面添加为父页面的第一个子页面
- node.js - 部署后无法使用 mongoose 访问 mongodb(nodejs、express、共享主机)