Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Object Literal has no doubt been established in recent years as one of the essential components to best JavaScript coding practices. However, I am not very sure about what is the best way to structure my codes using Object Literal.

It has been suggested earlier before that Module Pattern might be a good technique to structure your codes [1] [2], but criticisms regarding Module Pattern have begun to surface as people spent more times exploring the extent of the technique. [3] [4]

So, my question is: as of summer 2011, what is the acknowledged best way to structure your codes utilizing Object Literal? Is it still Module Pattern, or has some other technique emerged already as a yet better replacement?

share|improve this question

migrated from Aug 7 '11 at 16:49

This question came from our site for professional and enthusiast programmers.

I don't think object literals are what you think they are. – Lightness Races in Orbit Aug 7 '11 at 16:49

The module pattern can be nice for certain things, but you should really consider whether you really need your properties to be private. If it is really necessary to make your properties private, then by all means, use the Module pattern:

var namespace = (function(){ 
  var a = {
    internalFunction: function (number) {/*Do internal stuff*/},
    internalVariable: 365,
    publicFunction: function() { return internalFunction(internalVariable) },
  return {
    publicFunction: a.publicFunction

If you don't mind all of your functions being accessible, just use this simpler form:

var namespace = {
  noLongerInternalFunction: function(number) {/*Do stuff*/},
  noLongerInternalVariable: 365,
  stillPublicFunction: function() { 
    return noLongerInternalFunction(noLongerInternalVariable); 
share|improve this answer
You stole my suggestion, I want it back. On a serious note, there is hardly such thing as "Best JavaScript Coding Structure Using Object Literal", in fact there is no silver bullet. – Kumar Aug 7 '11 at 17:30
Thanks, then I guess this would give rise to another question: when should it be considered as "really necessary" to make something private? The only instance I could think of is that if you have some client-side code you don't want anyone else to be able to mess up with a greasemonkey script. What about server-side, and so on? – gsklee Aug 8 '11 at 1:15

This doesn't make much sense. There is no "Object Literal" design pattern, and "Object Literal" isn't a component.

Object literals (plural) are an inherent feature of Javascript syntax, and I don't see what relevance they have to any design pattern in particular or, in fact, design as a whole.

(Also, try not to worry too much about what patterns other people are using at any given arbitrary point in time. Just use the right tool for the job.)

share|improve this answer
Rebecca Murphey, who happens to be a pretty OK JS programmer, introduces the "object-literal pattern" as a way to mitigate the pervasiveness of spaghetti DOM manipulation most new (and many not-so-new) jQuery developers tend to write. – Scott Silvi Nov 13 '12 at 18:30
@Scott: In your view, is this what the OP was refering to? And, if so, does it deserve a prominent reference in the question body? – Lightness Races in Orbit Nov 13 '12 at 19:27
@LRiO - I agree that there is no official "Object Literal" pattern; I simply think it's important to differentiate that using Object Literals is a pretty common way to provide structure to JS - RMurphey's example may be jQuery specific, but whether you're using $("#foo") or document.getElementById('foo') I think the message is the same. – Scott Silvi Nov 13 '12 at 21:00
@ScottSilvi: I should clarify that I asked my previous question with no prejudice. I accepted your comment and simply wished to take it further. – Lightness Races in Orbit Nov 14 '12 at 0:10

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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