0

I have BaseEntity class that is inherited by Person class. How can I list base class properties (BaseEntity) in Person class.

class BaseEntity {
    _id: string;
    created: number;

constructor(id: string, created: number = Date.now()) {
    this._id = id;
    this.created = created;
    }
}

class Person extends BaseEntity {
    name: string;
    age: number;

constructor(id: string, name: string, age: number) {
    super(id);
    this.name = name;
    this.age = age;        
    }

    listBaseClassProperties() {
      //console.log(super); HOW CAN I DO THIS?
    }
}

When I tryied to console.log(super) in one of the derived class (Person) methods, I get undefined...

Itzhak Galitch
  • 366
  • 4
  • 19
  • This sounds like [an X/Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Why do you want to list *only* the base class's properties? – T.J. Crowder Jan 06 '19 at 13:31
  • 1
    *"When I tryied to `console.log(super)` in one of the derived class (Person) methods, I get undefined..."* That suggests you think that there are multiple objects being created when you instantiate a derived class. (You wouldn't be the first to think that! :-) ) There aren't. There's a **single** object created, which has the features of both the derived class and the base class. – T.J. Crowder Jan 06 '19 at 13:32
  • 1
    Could I get the properties of EntityBase class by any operators of JavaScript, like `Object.getPrototypeOf(EntityBase)`? – Itzhak Galitch Jan 06 '19 at 13:36
  • 1
    Instance properties don't usually exist on prototypes (and that's not how you'd get the prototype of `BaseEntity` instances). – T.J. Crowder Jan 06 '19 at 13:40
  • Possible duplicate of [*Get properties of a class using Typescript*](https://stackoverflow.com/questions/40636292/get-properties-of-a-class-using-typescript) – T.J. Crowder Jan 06 '19 at 13:41

1 Answers1

1

This sounds like an X/Y problem, but addressing the question actually asked:

The object you're using doesn't differentiate between properties it got from the base class's code and properties it got from the derived class's code. They're just properties of the object. If you want to get a list of the properties the base class adds during initialization, you could do this immediately after calling super(); to get the names of all of the own, enumerable properties on the object at that point:

this.baseProperties = Object.keys(this);

...assuming you don't use TypeScript's automatic property initialization (which yo don't in the code in the question).

Then in listBaseClassProperties:

console.log(this.baseProperties.map(name => ({name, value: this[name]}));

...or any of several other ways you might use the list of names you got from Object.keys.

Of course, code in the base class can add a property any time, but that's strongly discouraged by TypeScript's static typing system, so getting the ones that exist immediately after initialization is probably Good Enough™. ;-)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875