This is weird! I am struggling hard with the problem that javascript Proxy handler's apply trap does not get the Proxy itself passed as an argument, only the naked function. But I need to pass along some metadata to use for re-wrapping function results.
So I thought I could just use Object.create to make a specialization of the function to which I could stick additional information. But that doesn't work! And that's surprising!
function test() { return "hi!"; }
test() // 'hi!'
test1 = test // ƒ test() { return "hi!"; }
test1() // 'hi!'
test2 = Object.create(test) // Function {}
test2() // Uncaught TypeError: test2 is not a function
test3 = new Function([], "return 'lo!';") // ƒ anonymous() { return 'lo!'; }
test3() // 'lo!'
test2.prototype.constructor() // 'hi!'
test3.prototype.constructor() // 'lo!'
Object.getPrototypeOd(test2)() // 'hi!'
So I guess I can could help myself evaluate a function if I unwrapped it somehow:
while(fn instanceof Function && typeof fn != 'function')
fn = Object.getPrototypeOf(fn);
But that doesn't work if I just want to call it, i.e., make my special function transparent to any downstream user.
OK, here is a workaround, instead of using Object.create(fn) I can just wrap it:
fn = function() { return fn.apply(this, arguments); }
now I can stick my special metadata to this fn and I can also wrap it in a Proxy.
But my question is: what is the meaning of Object.create(fn) if you don't get an actual callable function?