For a long time, I'm finding myself checking every nested object if it exists or not.
e.g. if I need
someObject.innerThing.evenInnerer.superInerer.length
I have to do
let length = someObject && someObject.innerThing && someObject.innerThing.evenInnerer && someObject.innerThing.evenInnerer.superInerer && someObject.innerThing.evenInnerer.superInerer.length;
to prevent an exception.
So I thought about a way to make it simpler and use an external method (within the class...) like this -
const dataFetcher = (appName, failedFunc) => {
try {
let item = eval(appName);
return item;
} catch (e) {
failedFunc && failedFunc();
return undefined;
}
};
and do this =>
let length = dataFetcher("someObject.innerThing.evenInnerer.superInerer.length", optionalErrorFunc);
if(length == undefined)....
and it's cleaner and simpler (IMO).
Are there any downsides to this..?
Thanks