0

I am currently having a problem to which a potential solution is always returning something when trying to access an undefined property.

As an example consider this:

const obj = {}
obj.myMethod() // will throw an error as obj.myMethod is undefined
populateObj(obj)
obj.myMethod() // will work now that myMethod is defined

It is not possible to set obj.myMethod to some default value beforehand as the names of the properties of obj are unknown (their names are determined by an external server). So I was thinking of having obj return some default method whenever accessing an undefined property. I am guessing that there is no way of doing this in javascript but thought I'd ask just in case.

Thanks in advance!

Note: I know I could introduce a obj.get method to return either a default value or the one set in obj, however I am trying to make this work with object destructuring.

timgfx
  • 199
  • 2
  • 14

0 Answers0