It seems like you are missing a colon:
function Foo() { } Foo.prototype.alert = function() { alert(this); };
Here's the fiddle in which it works as you expect.
Actually, what happens is that the alert method is called immediately, and a new Foo instance is passed to it, and then the alert is called on the return value (which is undefined ):
Foo.prototype.alert = function() { alert(this); }(new Foo()).alert();
As @Nemoy mentioned, if you just use new Foo().alert() , you will get the expected behavior, because automatic colon insertion will contain a half in the place you want (the lack of a half-column doesnβt change the code value). And since the new operator has the highest priority, parentheses are not required.
James allardice
source share