Well, it doesn't mean you won’t have any errors anymore. But the ting with a Map
is it has a very readable API, which makes your code more readable right?
You are completely right that it can be done with if(obj.event)
. But if you expect an event is an available and inside event there is a property, you will have to check it like: if(obj.event && obj.event.prop)
which is in my opinion not so readable.
I personally prefer to read it like this: if(obj.has('event'))
or we use the Reflect APIif(Reflect.has(obj, 'event'))
. Or we could use optional chaining, but the browser support is not that high at the moment.
My story was not a about what is better, but my opinion about readability and predictability in our code when we deal with Objects.