Home > Enterprise >  Listing all possible properties of a class in javascript
Listing all possible properties of a class in javascript

Time:06-15

I want to list all possible class properties. Something like this:

class Rectangle {
  height;
  width;
  constructor(width) {
    this.width = width;
  }
}

getClassProperties(Rectangle) // ['height', 'width']

I want to be able to make this calculation give the class, not an instance of it. using hasOwnProperty \ getOwnPropertyName getPrototypeOf will only work on an instance.

CodePudding user response:

Modified answer from: https://stackoverflow.com/a/46237515/17954209

A general solution will be:

class A {
    private a1;
    private a2;
    constructor(a1:number, a2:string){
        this.a1 = a1;
        this.a2 = a2;
    } 
}

class Describer{    
   describeClass(typeOfClass: any){
       let a = new typeOfClass();
       let array = Object.getOwnPropertyNames(a);
       return array;//you can apply any filter here    
   }
}

Or alternatively in a function form

function getClassProperties(typeOfClass: any) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array; //you can apply any filter here   
}
// You tagged typescript in the question, but javascript in the actual title, 
// so here's the JS equivalent
function getClassProperties(typeOfClass) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array; //you can apply any filter here   
}

Basically at some point you'll have to create an instance of the class in order to run the constructor function to initialize the properties. You either have to initialize the property in the constructor, or while declaring it. Otherwise it will not work. It's explained well in the linked answer, quoted below. If you really don't define the property in the constructor but would like a workaround, simply set it to undefined or null in the constructor/declaration.

In Typescript

class A {
    private a1;
    private a2;


} 

Generates the following code in Javascript:

var A = /** @class */ (function () {
    function A() {
    }
    return A; }());

I would recommend reading more of the answer I modified from if you have any other questions.

If you want Typescript to give a return type as well you can use the handy ConstructorParameters utility type

function getClassProperties<T extends {new (...args: any): any}>(typeOfClass: T) {
    let a = new typeOfClass();
    let array = Object.getOwnPropertyNames(a);
    return array as ConstructorParameters<T>;
}

View on TS Playground

CodePudding user response:

ES6 classes are a "syntactic sugar" method and are essentially functions. In order to get the list of properties you have to run the function i.e. instantiate the object.

class Rectangle {
  height;
  width;
  constructor(width) {
    this.width = width;
  }
}

function getClassProperties(jsClass) {
  if (typeof jsClass !== "function") {
    throw Error("not a class");
  }

  return Object.keys(new jsClass);
}

console.log(getClassProperties(Rectangle));

  • Related