reflection - Typescript looping trough class type properties -
how can 1 loop through properties of class in typescript? take following class example:
export class task implements itask { id : number = 0; name: string; description: string; completed: boolean = false; tasktype: tasktype; }
im want retrieve properties, hence: ["id", name", "description", "completed", "tasktype"]
tried
gettaskheaders = () => { var _self = this; var thead = $('<thead />').append('<tr />'); for(var i=0; typeof todoapp.task.arguments; i++){ var th = $('<th />'); th.append(todoapp.task.arguments[i]); thead.append(th); } console.log(thead); return thead; }
unfortunately without success, know using "todoapp.task.arguments" incorrect. however, can show me right way please?
let's consider "not defined" properties i.e. properties defined in typescript class (i wrote "not defined" , not undefined
reason clear below)
class { prop1: string prop2: number }
will not enumerated of object.keys
or this.hasownproperty(k)
since autogen javascript has no knowledge of properties. have 1 option, when create typescript class, initialize properties default values like
class { prop1: string prop2: number prop3: b constructor() { this.prop1=""; this.prop2=-1; this.prop3=null; } }
at point properties of a
instance in mapping iteration dictionary
var = new a(); (var in properties) { if (a.hasownproperty(i)) { a[i]=properties[i]; } }
if don't default values solution, can still using magic undefined
javascript keyword do:
class { prop1: string = undefined prop2: number = undefined }
at point javascript counterpart have properties in model , iterate them object.keys(this)
or inspect them via this.hasownproperty
Comments
Post a Comment