3

Possible Duplicate:
How to convert an "object" into a function in JavaScript?

I have a theoretical question. As far as I know, {} objects come from Object, and functions inherit from that as well. Is there something user-accessible that can make {} objects callable, like functions, or is it interpreter magic?

I'm thinking of something like:

var myobj = {}
myobj["__call"] = function() {do_things();}
myobj();

and have it work. Is it possible? If not, why not?

Thanks!

Community
  • 1
  • 1
Stavros Korokithakis
  • 4,680
  • 10
  • 35
  • 42
  • 1
    I might be wrong but I think this is impossible due to the lack of operator overloading in javascript engines – sternr Aug 23 '11 at 14:51
  • 1
    http://stackoverflow.com/questions/124326/how-to-convert-an-object-into-a-function-in-javascript – Barış Uşaklı Aug 23 '11 at 14:55
  • I think you can´t do this because there is no way to store code in a variable. You have to define a new function which contains the code, and then you have a new function, what isn´t what you want :P – Van Coding Aug 23 '11 at 14:55
  • 1
    Search StackOverflow for [JavaScript callable objects](http://stackoverflow.com/search?q=JavaScript+callable+objects) to see some different techniques. – user113716 Aug 23 '11 at 15:02
  • Black Knight, that's very informative, thank you. – Stavros Korokithakis Aug 23 '11 at 15:06

1 Answers1

4

Is there a way to turn a JS object into a function?

No.

All functions are objects, but not all objects are functions. What is the meaning of "calling" an object, if it's not already a function?


Related: What is the difference between a function object and a callable object?

From my comment below (since this seemed particularly useful to the OP):

A function is callable if it has the internal [[Call]] method. See ECMA-262 §9.11 and §13.2.1, and Table 9 on p.34. But, AFAIK, there's nothing you can do to add the [[Call]] method to an object that doesn't already have one.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • This sounds like a religious answer. The meaning of "calling" an object is making it into a "callable". Since a function is an object, it means that it has something that makes it a callable, so, if the language/interpreter exposes it, we can just tack it on an object and make it a callable as well. – Stavros Korokithakis Aug 23 '11 at 15:07
  • 1
    As per the question I linked, a function is callable if it has the internal [[Call]] method. See [ECMA-262 §9.11 and §13.2.1](http://www.ecma-international.org/publications/standards/Ecma-262.htm), and Table 9 on p.34. But, AFAIK, there's nothing you can do to add the [[Call]] method to an object that doesn't already have one. – Matt Ball Aug 23 '11 at 15:10
  • That explains it much better, thank you. Then it's only a matter of the interpreter not exposing/being able to add the [[Call]] method, thanks. – Stavros Korokithakis Aug 23 '11 at 15:14
  • In addition, I think that exposing the [[Call]] method (as a property, say), would have the side effect of having to set a property to a function to instantiate a function. – Stavros Korokithakis Aug 23 '11 at 15:21
  • I don't know about "having to" so much as "being able to." The point is, just start with a `function` if you want it to be callable. – Matt Ball Aug 23 '11 at 15:28