Update: I think some people take this a bit too seriously: It’s a fun little experiment: nothing more, nothing less.

Sometimes you’re just lazy. Especially when it comes to typing. And while the round brackets in a function call may not seem like a whole lot of overhead, they do get tiresome if you have to type them over and over again… especially if you don’t think you need them since the function doesn’t return anything and needs no arguments: Something that’s awfully common in object-oriented UI programming.

Surprisingly enough, this isn’t all that difficult to do in Javascript, since a lot of methods are called implicitly… toString is probably the most famous one, but there’s also valueOf that does more or less the same but is called for numeric (and some other) casts.

The simplest way to call it is including a sign:

+myFunction;

which effectively translates to

window.parseFloat(myFunction.valueOf());

so all we need to do is provide a valueOf method that calls the function itself, like so:

Function.prototype.valueOf=function(){this.call(this); return 0;};

and suddenly the plus sing is all that’s needed for our call.

Especially in an object oriented environment you’ll have a lot of these calls, but you’ll have to fix the scope since these methods would be called in the scope of the method itself:

Function.prototype.fix=function(s){var t=this; return function(){ return t.apply(s,arguments); }; };

Using this method in the constructor to overwrite each method with a fixed-scope wrapper solves this:

var Foo=function(){ this.myMethod=this.myMethod.fix(this); };

or a bit more automated:

var Foo=function(){ for(var i in this) if(typeof(this[i])=="function") this[i]=this[i].fix(this); };

and finally we end up with this whole example (after a bit of OOP refactoring):

var StandardClass=function(){}; StandardClass.prototype.initMethods=function(){ for(var i in this) if(typeof(this[i])=="function" && this[i].dontWrap!==true) this[i]=this[i].fix(this); }; StandardClass.prototype.initMethods.dontWrap=true; Function.prototype.fix=function(s){ var t=this; return function(){ return t.apply(s,arguments); }; }; Function.prototype.valueOf=function(){ this.call(this); return 0; }; var Foo=function(name){ this.initMethods(); this.name=name; }; Foo.prototype=new StandardClass; Foo.prototype.showName=function(){ alert(this.name); }; Foo.prototype.showNameUpperCase=function(){ alert(this.name.toUpperCase()); }; var myFoo=new Foo("Hello World"); +myFoo.showName; +myFoo.showNameUpperCase;