Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a callable JS object with an arbitrary prototype? [duplicate]

Possible Duplicate:
Can a JavaScript object have a prototype chain, but also be a function?

I'm looking to make a callable JavaScript object, with an arbitrary prototype chain, but without modifying Function.prototype.

In other words, this has to work:

var o = { x: 5 };
var foo = bar(o);
assert(foo() === "Hello World!");
delete foo.x;
assert(foo.x === 5);

Without making any globally changes.

like image 260
Andrey Fedorov Avatar asked Feb 14 '09 04:02

Andrey Fedorov


People also ask

How to assign new object in JavaScript?

Input : var obj1 = { a: 10 }; var obj2 = { b: 20 }; var obj3 = { c: 30 }; var new_obj = Object. assign(obj1, obj2, obj3); console. log(new_obj); Output : Object { a: 10, b: 20, c: 30 } Explanation: Here in this example the properties of three source objects "obj1, obj2, obj3" are copied to the target object "new_obj".

What is callable object in JavaScript?

A callable object is a data structure that behaves as both an object and a function. You can access and assign properties obj.bar , call methods obj.foo() , but also call the object directly obj() , as if it were a function.

Which of the following object methods can be used to copy all of the properties of an object into a new object?

Static methods. Copies the values of all enumerable own properties from one or more source objects to a target object.


1 Answers

There's nothing to stop you from adding arbitrary properties to a function, eg.

function bar(o) {
    var f = function() { return "Hello World!"; }
    o.__proto__ = f.__proto__;
    f.__proto__ = o;
    return f;
}

var o = { x: 5 };
var foo = bar(o);
assert(foo() === "Hello World!");
delete foo.x;
assert(foo.x === 5);

I believe that should do what you want.

This works by injecting the object o into the prototype chain, however there are a few things to note:

  • I don't know if IE supports __proto__, or even has an equivalent, frome some's comments this looks to only work in firefox and safari based browsers (so camino, chrome, etc work as well).
  • o.__proto__ = f.__proto__; is only really necessary for function prototype functions like function.toString, so you might want to just skip it, especially if you expect o to have a meaningful prototype.
like image 90
olliej Avatar answered Oct 08 '22 18:10

olliej