Anyway, Jonathan, that is true. The modular pattern returns expandos though. In one sense, it really does serve an entirely different purpose. Eg: To get this:

var foo = function() {
  var bla = 'huzza';
  return {
    bar : function() {
      // cod here
    baz : function() {
      // code here

You'd have to do this (without the self-invoking funcitonality).

var foo = function() {
  var bla = 'huzzah';
foo.bar = function() {
  // code here
foo.baz = function() {
    // code here

But even then, you still don't have equal access to 'bla' through 'bar' and 'baz'. So as I mentioned, they're almost too different to compare.

Bramus! said on August 31, 2006

Always have been using the former, didn't even know about the latter one (*blush*), but now that I see it, I think it can be handy in some situations, but not all (cfr. Dustin's comment: "they're almost too different to compare")

Oh yeah, Dustin, FX ( doesn't crash here when using the pre tag ... maybe Jonathan already fixed it?

Olivier Mengu??? said on September 12, 2006

The two syntax execute the function, but in different contexts. When the function is executed, the difference appears in the content of the this variable. Below I've linked to the Mozilla documentation for more details.

new context", is an object creation, a prototype instantiation. Inside the function, this refers to the newly created object.

However, when just executing a function (anonymous or not) outside a "new context", this is not defined and eventually refers to an external this variable (the Window object when in a browser environment).

Also, the return value is not used in the same way. Just try the two following URLs in the location area of your browser:

javascript:new function(){return 'Z';}
javascript:(function(){return 'Z';})()

With the former you get a new object (converted to "[object Object]" for the representation in your browser window) while the later gives "Z".

Sorry, comments are closed for this post. If you have any further questions or comments, feel free to send them to me directly.