I am looking for a shorter way to make a JavaScript object that can be used as a function. For instance, I can do the following:
var A=function(){window.alert('Hello World');}
A.hello='World';
Notice that I can call A() or access A.hello.
Is there a way to accomplish this using curly braces to create the object?
var A={
?:function(){window.alert('Hello World');},
hello:'World',
};
As you know a function is an Object. Nothing prevents you from writing properties in the function itself.
If you want to access the properties written in the function from the function itself, you must use the ‘callee’ attribute of the ‘arguments’: