関数プロキシで .toString() を呼び出そうとしています。
関数プロキシを作成して toString を呼び出すだけで「TypeError: Function.prototype.toString is not generic」が発生し、toString を元のソースを返すように設定すると「RangeError: Maximum call stack size exceeded」が発生しますが、toString の get トラップが作成されます。動作します。
toString 関数を設定するだけでは機能しないのに、get トラップを作成すると機能するのはなぜですか?
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()