0

Im not really getting into the details of WHY doing this as its needed.

Supose i have a simple object.

var obj = {};

Lets say i want to access a random property of that object

obj.randomProp

This is getting me undefined. Now, would it be possible, for that obj.randomProp return me another empty object ( {} ) ?

Of course, with no property declaration as obj.randomProp=123. I was thinking on this to be dynamic, somehow like a default property getter as if everytime you try to get a property of this element, you will get a new empty object for it.

Is this possible in JS ?

Gabriel Slomka
  • 1,487
  • 1
  • 11
  • 24
  • Yes, with Proxies. – dfsq Mar 23 '18 at 16:02
  • Possible duplicate of [Set default value of javascript object attributes](https://stackoverflow.com/questions/6600868/set-default-value-of-javascript-object-attributes) – JJJ Mar 23 '18 at 16:05
  • It is a duplicate so far, ive read about proxies now and its what im looking for, but i didnt managed to make a `infinite` proxy – Gabriel Slomka Mar 23 '18 at 16:08
  • What is infinite proxy? – dfsq Mar 23 '18 at 16:10
  • If you want to return by default an object that does exactly the same thing as the original, just return a copy of the original. – JJJ Mar 23 '18 at 16:12

0 Answers0