As I wrote in my comment, you cannot make the object "callable". However, you can automate the process from the first example:
function extend(func, props) {
for(var prop in props) {
if(props.hasOwnProperty(prop)) {
func[prop] = props[prop];
}
}
return func;
}
and then call it with:
var test = extend(function(){
console.log(test.data);
},
{
data: 'hello',
set: function (data) {
this.data = data;
}
});
Demo
However, I think you should not use such functions. This will be easier to understand if you just have a “normal” object and call each method with obj.method()instead obj().
, .