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.
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".
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.
Static methods. Copies the values of all enumerable own properties from one or more source objects to a target object.
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:
__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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With