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

Object types are functions and an object itself is a function instantiation.

alert([Array, Boolean, Date, Function, Number, Object, RegExp, String].join('\n\n'))

displays (in FireFox):

function Array() {
    [native code]
}

function Boolean() {
    [native code]
}

function Date() {
    [native code]
}

function Function() {
    [native code]
}

function Number() {
    [native code]
}

function Object() {
    [native code]
}

function RegExp() {
    [native code]
}

function String() {
    [native code]
}

In particular, note a Function object, function Function() { [native code] }, is defined as a recurrence relation (a recursive definition using itself).

Also, note that the answer 124402#124402 is incomplete regarding 1[50]=5. This DOES assign a property to a Number object and IS valid Javascript. Observe,

alert([
  [].prop="a",
  true.sna="fu",
  (new Date()).tar="fu",
  function(){}.fu="bar",
  123[40]=4,
  {}.forty=2,
  /(?:)/.forty2="life",
  "abc".def="ghi"
].join("\t"))

displays

a   fu  fu  bar 4   2   life    ghi

interpreting and executing correctly according to Javascript's "Rules of Engagement".

Of course there is always a wrinkle and manifest by =. An object is often "short-circuited" to its value instead of a full fledged entity when assigned to a variable. This is an issue with Boolean objects and boolean values.

Explicit object identification resolves this issue.

x=new Number(1);  x[50]=5;  alert(x[50]);

"Overloading" is quite a legitimate Javascript exercise and explicitly endorsed with mechanisms like prototyping though code obfuscation can be a hazard.

Final note:

alert(  123 . x = "not"  );

alert( (123). x = "Yes!" );  /* ()'s elevate to full object status */

Use a temporary variable:

var xxx = function()...

then copy all the properties from the original object:

for (var p in bar) { xxx[p] = bar[p]; }

finally reassign the new function with the old properties to the original variable:

bar = xxx;

There doesn't appear to be a standard way to do it, but this works.

WHY however, is the question.

function functionize( obj , func )
{ 
   out = func; 
   for( i in obj ){ out[i] = obj[i]; } ; 
   return out; 
}

x = { a: 1, b: 2 }; 
x = functionize( x , function(){ return "hello world"; } );
x()   ==> "hello world" 

There is simply no other way to acheive this, doing

x={}
x() 

WILL return a "type error". because "x" is an "object" and you can't change it. its about as sensible as trying to do

 x = 1
 x[50] = 5
 print x[50] 

it won't work. 1 is an integer. integers don't have array methods. you can't make it.


It's easy to be confused here, but you can't (easily or clearly or as far as I know) do what you want. Hopefully this will help clear things up.

First, every object in Javascript inherits from the Object object.

//these do the same thing
var foo = new Object();
var bar = {};

Second, functions ARE objects in Javascript. Specifically, they're a Function object. The Function object inherits from the Object object. Checkout the Function constructor

var foo = new Function();
var bar = function(){};
function baz(){};

Once you declare a variable to be an "Object" you can't (easily or clearly or as far as I know) convert it to a Function object. You'd need to declare a new Object of type Function (with the function constructor, assigning a variable an anonymous function etc.), and copy over any properties of methods from your old object.

Finally, anticipating a possible question, even once something is declared as a function, you can't (as far as I know) change the functionBody/source.