How do you write a function lazyDeepMerge(object1, object2) which constructs and returns some object lazyObject such that whenever lazyObject is queried for a the value of a property someProp, the value returned behaves exactly the same as the value returned by calling regularDeepMerge(object1, object2).someProp?
As background, deep merging two objects (recursively merging their respective properties) has been addressed here: How to deep merge instead of shallow merge?
The difference between lazyDeepMerge and regular deepMerge() would be that the lazy one would not do any merging of values in advance.
For example, calling lazyObject.vehicles.camry.color would merge what’s needed in order to return a value identical to the one returned from calling eagerObject.vehicles.camry.color.
The process would be that calling lazyObject.vehicles would return an object lazyVehicles. If you then called lazyVehicles.camry.color, lazyVehicles would lazily merges what's necessary to return the value, and that value may be another lay object, etc.
Any solution would probably have to make use of the JavaScript Proxy object, which is very straightforward to use - so that’s not an issue I need to solve.
An ideal solution would involve caching the values of properties the first time they’re looked up, as well as the ability to pass in a custom merge(obj1, obj2) function (like with the Lodash library function _.mergeWith(object, sources, customizer))