I was using proxy get
method fine. Then I tried using it on a function and quickly realized I needed to use the apply
method. This simple example i
You assume here that apply
traps work like get
traps -- i.e., for any property of the proxied object -- but they do not. The apply
trap runs when the Proxy itself is called a function. Here, your proxy is Magic.Foo
, but you never call Magic.Foo()
. You only call Magic.Foo.runme()
, which is not a proxied function.
You must wrap each function whose invocation you wish to intercept in its own individual Proxy
wrapper.
Alternatively, you could use a get
trap on Magic.Foo
that returns a function with the appropriate behavior for each accessed property.
The apply
trap only applies to proxies over functions, which create callable objects (i.e. functions). A new Proxy
created on a normal object (like your {}
) creates a non-callable proxy object, which will never trigger apply
.