Skip to content
Advertisement

How to check and access a function from another object using JavaScript Proxy?

I am practicing JavaScript Proxies and want to access a method from another object but with an empty Proxy object:

JavaScript

Basically I’m trying to check if that method property exist in another object. I just want to tell the proxy to get the value from another object without passing it into the constructor.

Advertisement

Answer

I don’t understand why you want to use a different object than the object you are proxying. It makes little sense to proxy an empty object instead.

Secondly, if you are going to access the hello property of the proxy object, then realise that this hello value — as found in the data object — is not proxied, so it is not in this proxy that you should check whether the property is a function. At this point the log property is not accessed yet. It’s only about the hello access that the proxy is aware.

But you can divert the path to the data object when hello is accessed, by verifying it is indeed a property of the data object. So:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement