Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
Can a javascript Function object be called?

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?

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda