I would like to create a proxy object that intercepts all property retrievals and function calls, and returns undefined.
Accomplishing the former is trivial:
const proxy = new Proxy({}, { get: () => undefined });
proxy.foo // undefined
In fact, I don't even need a proxy for this, an empty object ({}) will do the same. However, doing the same for function calls throws an error:
proxy.foo() // TypeError: proxy.foo is not a function
I could have the get trap return a function:
const proxy = new Proxy({}, { get: () => () => undefined });
proxy.foo() // undefined
proxy.foo // [Function]
But as you can see, it will return a function instead of undefined for my original property access case. Is it possible to accomplish what I want?
const proxy = ???;
proxy.foo; // undefined
proxy.foo(); // undefined
The end goal is to use this as a "catch-all" stub for testing.
UPDATE: Thanks to @Bergi for his answer. The following code accomplishes what I want:
const proxy = new Proxy(new Function, {
get: (target, prop, receiver) => receiver,
});
console.log('proxy.foo', proxy.foo); // undefined
console.log('proxy.foo()', proxy.foo()); // undefined
console.log('proxy.foo.bar', proxy.foo.bar); // undefined
console.log('proxy.foo.bar()', proxy.foo.bar()); // undefined