0

If I have a class:

class MyClass {

  constructor(props?: any) {
    ...
  }
}

How would I check if props?.my_val is undefined and set a value for it if that is the case?

SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
  • 1
    I recommend **not** mutating function arguments, but if you insist `props.my_val ??= whatever` which is to say use the [nullish coalescing assignment operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_assignment). Note that in your case *props itself* might be missing. – Jared Smith May 06 '23 at 20:23
  • Does this answer your question? [Javascript object properties - create property if not exist](https://stackoverflow.com/questions/34419057/javascript-object-properties-create-property-if-not-exist) – Jared Smith May 06 '23 at 20:27
  • Please add an `new MyClass(???)` and constructor body example how you imagine it to be – Dimava May 06 '23 at 20:57
  • 1
    @JaredSmith thanks. I can work with that if I do a check for props existence first. :) – SomeDutchGuy May 06 '23 at 22:27

0 Answers0