Tuesday, June 4, 2024
43
rated 0 times [  44] [ 1]  / answers: 1 / hits: 33826  / 14 Years ago, fri, september 24, 2010, 12:00:00

I would like to know if the module pattern or Constructor/protoType pattern is more applicable to my work.



Basically I am using unobtrusive javascript -- the HTML document has a reference to the .js file.



My understanding of the module pattern:




  • call an INIT method (which is basically a public method i can create and return using the module pattern)

  • In the INIT method, assign all click events etc.



This sounds like the perfect pattern for my situation, as I don't need to create Objects and inheritance hierarchies etc.



My understanding of the Constructor/Prototype pattern:




  • for creating objects

  • for using inheritance (i.e. Subtypes of a supertype)



Am I correct, that for providing unobtrusive javascript, the module pattern is ideal?


More From » design-patterns

 Answers
14

Constructor-functions and prototypes are one of the reasonable ways to implement classes and instances. They don't quite correspond to that model so you typically need to choose a particular scheme or helper method to implement classes in terms of prototypes. (Some background on classes in JS.)



The module pattern is typically used for namespacing, where you'll have a single instance acting as a store to group related functions and objects. This is a different use case from what prototyping is good for. They're not really competing with each other; you can quite happily use both together (eg put a constructor-function inside a module and say new MyNamespace.MyModule.MyClass(arguments)).


[#95505] Thursday, September 23, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anabellejaynav

Total Points: 176
Total Questions: 105
Total Answers: 105

Location: Croatia
Member since Fri, Sep 11, 2020
4 Years ago
anabellejaynav questions
;