Javascript: module template vs constructor / prototype template?

I would like to know if the template model or constructor / prototype template is more applicable for my work.

I mainly use unobtrusive javascript - the HTML document has a link to the .js file.

My understanding of the module template:

  • calling the INIT method (which is basically an open method that I can create and return using the module template)
  • In the INIT method, assign all click events, etc.

This sounds like the perfect template for my situation, since I don't need to create objects and inheritance hierarchies, etc.

My understanding of the constructor / prototype template:

  • to create objects
  • to use inheritance (i.e. supertype subtypes)

Is it right that a module template is perfect for providing unobtrusive javascript?

+69
javascript unobtrusive-javascript design-patterns module-pattern
Sep 24 '10 at 20:54
source share
4 answers

Constructor functions and prototypes are one of the smartest ways to implement classes and instances. They do not quite correspond to this model, so you usually need to choose a specific circuit or helper method for implementing classes in terms of prototypes. ( Some class information in JS .)

A module template is typically used for a namespace, where you will have one instance acting as a repository for grouping related functions and objects. This is another use case from which prototyping is suitable. They do not compete with each other; you can use both together with pleasure (for example, put the constructor function inside the module and say new MyNamespace.MyModule.MyClass(arguments) ).

+64
Sep 24 '10 at 21:16
source share

The module layout is much simpler and more elegant than the prototype. However, first thinking of a mobile phone. This does not match the pattern for medium / large objects, because initialization must analyze the entire block before starting. Multiple closures also create circular dependencies that the garbage collector does not release (especially IE), this leads to the fact that a heavier amount of memory is not freed until the window (or tab) is closed - check the chrome task manager for comparison - Download time is inversely proportional to the size of the object using a module template, while this is not the case for prototype inheritance. The above statements are verified using several benchmarks, such as: http://jsperf.com/prototypal-performance/54

As seen from the last test. Small objects are better initialized as a simple object (without these patterns). It is suitable for single objects that do not require closure or inheritance. It’s prudent to evaluate if you need these templates.

+10
Dec 03
source share

You can try the Foldable Template, here is the link: Javascript Folding Pattern

I also left an answer to a similar question, which shows how to use the folded template:

A simple example of a folding pattern

+4
Apr 07 '13 at 0:53
source share

The prototype template helps us expand functionality and there is only one instance of functions in memory, regardless of the number of objects. In the patter module, each object creates a new instance of the functions in memory, but it provides the concept of private / public variables and helps to encapsulate variables and functions.

+4
Jul 16 '14 at 18:16
source share



All Articles