I am trying to call .toString () for a proxy function.
Simply creating a proxy function and calling toString raises “TypeError: Function.prototype.toString is not common”, setting toString to return the source of the original reasons “RangeError: Maximum call stack size”, but creating a get trap for toString to work.
Why just setting the toString function does not work, but set a trap for receiving?
function wrap(source) { return(new Proxy(source, {})) } wrap(function() { }).toString()
function wrap(source) { let proxy = new Proxy(source, {}) proxy.toString = function() { return(source.toString()) } return(proxy) } wrap(function() { }).toString()
function wrap(source) { return(new Proxy(source, { get(target, key) { if(key == "toString") { return(function() { return(source.toString()) }) } else { return(Reflect.get(source, key)) } } })) } wrap(function() { }).toString()
javascript tostring ecmascript-6 es6-proxy
Daniel Herr
source share