V
V
Vladimir Golub2020-11-20 16:13:14
typescript
Vladimir Golub, 2020-11-20 16:13:14

How to check if a property exists in a class?

I want to check a property in a class and set its value to ?

export class Test {
  public name: string;
  public type: string;

  constructor(props) {
    Object.keys(props).map(item => {
      if(this.hasOwnProperty(item)) {
        this[item] = props[item]
      }
    })
  }
}

    const test = new Test({ name: 'Dima' });

    console.log(test);


The object is created but empty.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question