0

I want to proxy some built in object primitives in JS, for example, “Number” or “String” object wrappers. However it seems the Proxy object ends up reporting as a normal JS object when passed to some native functions.

const n = new Number(10);
const p = new Proxy(n, {});
console.log(Object.prototype.toString.bind(n)()); // [object Number]
console.log(Object.prototype.toString.bind(p)()); // [object Object]

This is just one example where the proxy gets treated differently than its target. This different treatment seems to happen whenever the proxy is passed to a function implemented in native code.

Is it possible to get the proxied object to be treated as its target object even when passed to such functions like Object.protocol.toString?

3
  • "I want to proxy some built in object primitives in JS" - just curious, but why? Why are you even using primitive object wrappers? Commented Dec 20, 2022 at 6:37
  • @Bergi I found a workaround. I set the prototype of a builtin object to a proxy which was enough for my situation. And I'm trying to implement JS in JS for fun. I'd love help if you're interested github.com/joehinkle11/SandboxedJS Commented Dec 20, 2022 at 7:27
  • @Bergi for testing, it can be convenient to intercept properties of an object. Trying to set the prototype of Object.prototype with a Proxy is not possible. Is there another way? Commented May 3, 2023 at 0:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.