1

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?

Gunther Schadow
  • 1,490
  • 13
  • 22
  • Does this answer your question? [JavaScript: clone a function](https://stackoverflow.com/questions/1833588/javascript-clone-a-function) – pilchard Jul 18 '22 at 08:06
  • and to answer the last question, function type is not an object type.. so a variable holding a function won't have the object prototype you are expecting to instantiate your new object using Object.create(). Also your latest workaround is pretty messy.. using inside the function a variable declared out of its scope holding the function itself. You just need an object holding your function and your metadata.. or a function wrapper containing the call to that target function with arguments wired in the invocation – Diego D Jul 18 '22 at 08:09
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create "The Object.create() method creates a new object, using an existing object as the prototype of the newly created object." Using `z = Object.create(f);`, `z` will be an object (not function) that "inherits" the properties of `f`. So, `z.call` will be `ƒ call() { [native code] }` (inheriting `f.call`) and so on. But if you try to actually use them directly, e.g. in Chrome, you'll get errors, e.g. `z.toString ()` : `Uncaught TypeError: Function.prototype.toString requires that 'this' be a Function` – qrsngky Jul 18 '22 at 08:26

1 Answers1

1

Short answer: Function can be called; Object cannot be called.

See Function like inheritance of Object. Functions are object but objects are not function.

See this:

function test() { return "hi!"; }
test()                                    // 'hi!'
typeof test                               // 'function'
test instanceof Object                    // true

test2 = Object.create(test)               
typeof test2                               // 'object'
test2 instanceof Object                    // true
typeof test2.prototype.constructor         // 'function'

// something else that works
sample1 = Object.create({test})               
typeof sample1                             // 'object'
typeof sample1.test                        // 'function'
sample1.test()                             // 'hi!'

If you insist to make an Object callable, maybe your closest option is to create your own class that have function behaviour. There're some suggestions here: Can you make an object 'callable'?

zhuhang.jasper
  • 4,120
  • 4
  • 21
  • 29