OOP with JavaScript

Joined: 11/28/2008

Up until now, I've always created objects/namespaces with Object Literal (or so I think it is...) like this:

CODE
var ES = {
   Method : function() {
              // do something
   },
   Something : function() {
              // do something
   }
}

Lately, I've been seeing a lot of different ways to create objects, and I've been getting fairly confused.

I see Dustin doing stuff like this a lot:

CODE
var ES = window.ES || {};
ES = function() {
  return {
      Method : function() {
                   // do something
      },
      Something : function() {
                         // do something
      }
  };
}();

Then I've seen things like this:

CODE
var ES = new function() {
      Method : function() {
                   // do something
      },
      Something : function() {
                         // do something
      }
  };
};

What's the difference between all of these? I've been testing them all out, and I can't really figure it out.

I have noticed that the "var ES = window.ES || {};" line in my second piece of code doesn't seem to be necessary, and neither does declaring ES as a variable in the first piece of code.

Can anybody help a guy out? Sort of showing my ignorance a little bit here...

Joined: 11/28/2008
It's normal to be confused

It's normal to be confused about all this JS OOP Stuff. to answer all your questions, here it is in a nutshell.
In all your cases, they are essentially singletons... which don't exactly apply all the methodologies behind OO practices - since the purpose of reusable objects is allowing yourself to have multiple instances.

In any case, you do get the 'class-like' feature of it all (eg: confinement (scope)).

Re:

CODE
var ES = window.ES || {};

That is a temporary identifier. the "||" is the default operator. The left side be assigned to var ES if it is found already in the document - and the right side if it isn't. What I generally use it for is to safe guard my code for any cases where I have already defined my global object (for instance, in another file). If I haven't defined it yet, it will "default" to just an empty object {}.
Re:

CODE
var foo = new function() {
  // whatever
};

This is deprecated JavaScript in favor of assigning an anonymous function to a variable which is then called a "function literal."

Re:

CODE
var foo = function() {
  return {
     // public methods
   };
}();

I wrote a good little article on namespacing your JavaScript just a bit back.

Joined: 11/28/2008
OK, that makes a little more

OK, that makes a little more sense...thanks. Are there any benefits to using one over the other? Like why would you want to use

CODE
var foo = function() {
  return {
     // public methods
   };
}();

over something like

CODE
var foo = {
     // public methods
}

The first one takes more space, and don't they do the same thing?

Joined: 11/28/2008
with the first you get the

with the first you get the advantage of storing private methods that you wouldn't want to be overridden from the Subscribers of your code (assuming you're the producer). It's a way of bullet proofing your code. Some (like snook don't think private vars are all that needed in the JavaScript language. So do what you're most comfortable with. In anycase, I like them.

See private, public, and priviledged