5

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?

1

2 Answers 2

5

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); 
  }
}
2
  • 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, 2011 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, 2011 at 1:15
4

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.)

4
  • 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. Nov 13, 2012 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? Nov 13, 2012 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. Nov 13, 2012 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. Nov 14, 2012 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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