Septagram Septagram - 1 year ago 95
Javascript Question

Can you make an object 'callable'?

Is it possible to make an object callable by implementing either

on it, or in some other way? E.g.:

var obj = {}; = function (context, arg1, arg2, ...) {


obj (a, b);

Max Max
Answer Source

No, but you can add properties onto a function, e.g.

function foo(){}
foo.myProperty = "whatever";

EDIT: to "make" an object callable, you'll still have to do the above, but it might look something like:

// Augments func with object's properties
function makeCallable(object, func){
    for(var prop in object){
            func[prop] = object[prop];

And then you'd just use the "func" function instead of the object. Really all this method does is copy properties between two objects, might help you.