Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
89
rated 0 times [  94] [ 5]  / answers: 1 / hits: 29804  / 11 Years ago, sat, october 12, 2013, 12:00:00

Is it possible to make an object callable by implementing either call or apply on it, or in some other way? E.g.:



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

...

obj (a, b);

More From » javascript

 Answers
64

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){
if(object.hasOwnProperty(prop)){
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, but...it might help you.


[#75036] Friday, October 11, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
helenat

Total Points: 450
Total Questions: 95
Total Answers: 97

Location: Central African Republic
Member since Mon, Aug 10, 2020
4 Years ago
;