3

I've been thinking, if there was a way of avoiding

if (this.a && this.a.hasOwnProperty('b') && this.a.b.hasOwnProperty('c')) {
    // this.a.b.c exists
}

and making it a lot easier to code and read like

if ('b.c' in this.a) {
    // same, this.a.b.c exists
}

or

if ('a.b.c' in this) ...

Its okay that thats not much pain to write a few more characters, but there may be more complex objects needed to be checked in every level to make sure they're set. A few years ago I wouldnt even have thought a way like this was possible, but recently as ES6 is here and everything gets easier, I'm hoping for a good solution.

EDIT:

Thanks for linking the post that's close to what I asked, haven't seen that one. However, I've seen some others similar to it. As its 7 years old, I'm not sure its recent, and still would like to ask for new-way solutions if there's any.

Kiss Koppány
  • 919
  • 5
  • 15
  • 33
  • 1
    note you have a proposal (currently in stage 1) https://github.com/tc39/proposal-optional-chaining for optional chaining. So if you use a transpiler such as Babel you can use such pattern and have your code transpiled – laurent Dec 25 '17 at 22:06
  • exactly what I was looking for! Thanks @laurent, I'd accept your answer if there was a way for that. – Kiss Koppány Dec 27 '17 at 21:39

0 Answers0