jeudi 21 octobre 2021

Nodejs function - print properties of a class using reflection

I need help with writing a javascript function that gets a class as a parameter, and prints all its public properties (name and value) using reflection with indentation.

Some properties can be of type class so the properties need to be printed with the correct indentation.

Example:

Class A {
  a1;
  a2;

  constructor() {
    this.a1 = 'a';
    this.a2 = 2;
  }
}


Class B {
  b1;
  b2;

  constructor() {
    this.b1 = true;
    this.b2 = new A();
  }
}

When getting class B as a parameter the output should be:

{
  b1: true,
  b2: {
    a1: a,
    a2: 2
  }
{

Thank you!





Aucun commentaire:

Enregistrer un commentaire