Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is there a preferred way of doing what I describe here. I want to namespace my JavaScript code and use the Singleton Pattern so that only one instance of my JavaScript app exists.

I have seen two approaches to this in JavaScript. One which simply returns and object literal, and another which uses the Revealing Module Pattern. If all I want is a singleton object (not caring about enforcing faux public/private methods and properties) is returning an object literal the way to go?

// Approach 1
var AppOne = (function(){
  return {
    Util: {
      init: function() {
        $("body").append("<strong>AppOne.Util initiliased</strong><br>");
      }
    },
    Ajax: {
      init: function(){
        $("body").append("<strong>AppOne.Ajax initialised</strong><br>");
      }
    }
  };
})();

// Approach 2
var AppTwo = (function(){
  var Util = function() {
    return {
      init: function() {
        $("body").append("<strong>AppTwo.Util initialised</strong><br>");
      }
    };
  }();

  var Ajax = function() {
    return {
      init: function() {
        $("body").append("<strong>AppTwo.Ajax initialised</strong><br>");
      }
    };
  }();

  return {
    Util: Util,
    Ajax: Ajax
  };
})();

AppOne.Util.init();
AppOne.Ajax.init();

AppTwo.Util.init();
AppTwo.Ajax.init();
share|improve this question
1  
This question appears to be off-topic because it doesn't have actual code to review and asks for design patterns. It might be better suited for programmers.stackechange.com. –  amon Oct 20 '13 at 11:04
 
Singleton makes no sense in JS IMHO. There is no Class in JS, so a notion of "single instance" is absurd... If you want a single object, just use an object litteral. –  m_x Oct 20 '13 at 11:08
1  
... the only difference i can see between approach 1 & 2 is that second approach allows more control about what is exported and what is in the closure. That's more or less require.js' approach –  m_x Oct 20 '13 at 11:18
 
For advantages and downsides I recommend reading: addyosmani.com/resources/essentialjsdesignpatterns/book/… –  Thomas Junk Oct 20 '13 at 22:06
1  
@zzzzBov pretty code, but meh. This still doesn't make sense to me. JS is prototype-based, so I don't see the point of "simulating class-based inheritance so that I can simulate the singleton pattern" –  m_x Oct 22 '13 at 15:56
show 2 more comments

closed as primarily opinion-based by codesparkle Oct 26 '13 at 12:40

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

To javascript there is absolutely no difference between the two approaches.

I would recommend the latter as it gives you more options for code organization.

Also, this question is about object construction and has nothing to do with singletons (as is evidenced by the fact that you create two versions of your app).

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.