How to Proxy Built-In JS Object?

163 Views Asked by At

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?

0

There are 0 best solutions below