up vote 3 down vote favorite
share [g+] share [fb]

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?

link|improve this question
1  
I don't think object literals are what you think they are. – Lightness Races in Orbit Aug 7 '11 at 16:49
feedback

migrated from stackoverflow.com Aug 7 '11 at 16:49

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

2 Answers

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

link|improve this answer
feedback

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); 
  }
}
link|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
feedback

Your Answer

 
or
required, but never shown

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