I want to create an infinite depth 'anything' object that has the following two properties:
- Every property on the anything object is another anything object.
- Every property on the anything object is also a function that returns an anything object.
The purpose is for simple mocking of objects in tests when a more specific mock is not needed.
Property 1 is fulfilled by How do I make an object that has infinite depth through arbitrary properties in Javascript? but it is missing property 2. The solution must be in ES5 for runtime environment reasons.
var p = infiniteDepthObject();
// All of the following should be defined for arbitrary property names at any depth
p.foo
p.foo.bar
p.foo().bar
p.foo().bar()
p.foo.bar()
p.foo.bar.baz
p.foo.bar.baz()
p.foo.bar().baz
p.foo().bar.baz
p.someOtherPropertyChosenAtRunTime()
... etc
Can this be done in ES5 without a polyfill for Proxy?
EDIT: Not a duplicate of How does basic object/function chaining work in javascript?. I know I can return 'this' from a function to chain. The question is about defining all possible properties to be 'chain' methods. That is p.whateverYouCanImagine() returns p and also p.anyOldPropertyName as a property access returns p OR in both cases returns a new object that performs the same function as p. Not chaining, but some form of chaining may solve this problem. The difficult/different part is that at runtime I may choose a property to access that is not written into the object and it should still work.