Is it possible to construct an object in a way to return the following:
baseObject.foo; // return a string describing what foo does
baseObject.foo(); // execute the foo function
baseObject.bar; // bar description
baseObject.bar(); // execute bar
Update: JavaScript Method Overloading is close, but not quite what I’m looking for.
Update2: jsfiddle (of solutions provided below).
baseObject.foohas to be a function, otherwise you won’t be able to call it. The only thing you can influence is how this function is coerced to a string, by overriding itstoString()method. By defaulttoString()returns function source code, you can make it return something else instead.