Can a javascript Function object be called?

187 views Asked by At

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?

1

There are 1 answers

0
zhuhang.jasper On

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'?