Questions tagged [module-pattern]

Popular JavaScript pattern, var Module = (function() { ... })();

Module pattern helps to avoid namespace conflicts, and thus is widely use by various third-party scripts, like libraries or banners.

Major drawbacks of using it are: much more difficult debugging and low IDE support.

var Module = (function ( ) {
      ...

})();

Original presentation at YUI blog. More detailed explanation by Ben Cherry.

Critique: by Jonathan Snook and Ed Spencer

234 questions
78
votes
3 answers

Javascript: Module Pattern vs Constructor/Prototype pattern?

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…
Martin
  • 23,844
  • 55
  • 201
  • 327
68
votes
4 answers

JavaScript design pattern: difference between module pattern and revealing module pattern?

I'm reading the book Learning JavaScript Design Patterns recently. What I don't get is the difference between module pattern and revealing module pattern. I feel they are the same thing. Anyone can give an example?
60
votes
3 answers

What is the intention of Ninject modules?

I'm a complete newbie to ninject I've been pulling apart someone else's code and found several instances of nInject modules - classes that derive from Ninject.Modules.Module, and have a load method that contains most of their code. These classes are…
Jonathan
  • 32,202
  • 38
  • 137
  • 208
53
votes
3 answers

Strict Violation using this keyword and revealing module pattern

Having trouble getting the following to pass jslint/jshint /*jshint strict: true */ var myModule = (function() { "use strict"; var privVar = true, pubVar = false; function privFn() { return this.test; // -> Strict…
Matty F
  • 3,763
  • 4
  • 30
  • 48
39
votes
6 answers

What is this code construct wrapping the parts of a library and what is it useful for?

I imitated a library and was able to write following code. This code created 'c' object to which 'a' function is assigned. So, to call 'a', I will have to write c.a(). Also, I was able to add more functions to this 'c' object. I want to understand…
Atul
  • 874
  • 1
  • 7
  • 17
25
votes
2 answers

Javascript revealing module pattern, public properties

I'm trying to get my head round the revealing module pattern in javascript. I'm puzzled by two things about the following code snippet. var Child = function () { var totalPoints = 100; addPoints = function (points) { …
centralscru
  • 6,580
  • 3
  • 32
  • 43
24
votes
1 answer

Understanding how JS Module Pattern works

I'm trying to understand js module patterns in use with jQuery. I've edited this a couple of times and will try to end up with a good practice for my skill level (a couple of months fresh on jquery). There's no direct question in this post. I'm more…
olemarius
  • 1,124
  • 4
  • 17
  • 27
18
votes
3 answers

Using the Module Pattern for larger projects

I'm interested in using the Module Pattern to better organize my future projects. Unfortunately, there are only a few brief tutorials and proof-of-concept examples of the Module Pattern. Using the module pattern, I would like to organize projects…
Rob Gibbons
  • 1,583
  • 3
  • 14
  • 24
15
votes
5 answers

Creating Multiple Instances of a Module

I thought I was starting to understand JavaScript quite well but clearly not. Let me explain my problem with an example. First I have the following module defined: var Test = function() { var counter = 0; function init() { …
nfplee
  • 7,643
  • 12
  • 63
  • 124
15
votes
3 answers

What is the difference between these three module pattern implementations in JavaScript?

I've seen the following three code blocks as examples of the JavaScript module pattern. What are the differences, and why would I choose one pattern over the other? Pattern 1 function Person(firstName, lastName) { var firstName = firstName; …
Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119
14
votes
2 answers

Javascript Module Pattern Events and Listeners

I'm implementing the module pattern, and would like to know the best/preferred way to define and register event listeners/handlers. The following works, but maybe there is a better/simpler way... var MODULE = function() { // private var…
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
14
votes
5 answers

JavaScript Module Pattern - What about using "return this"?

After doing some reading about the Module Pattern, I've seen a few ways of returning the properties which you want to be public. One of the most common ways is to declare your public properties and methods right inside of the "return" statement,…
Rob Gibbons
  • 1,583
  • 3
  • 14
  • 24
14
votes
5 answers

how to use the javascript module pattern in a real example?

I am trying to understand the JavaScript Module Pattern. I've seen examples of what it should look like, but I don't understand how to use it. For example, a few things are happening here: $('input#share').on("click", function() { …
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
13
votes
2 answers

JavaScript module pattern: How do private methods access module's scope?

When implementing the module pattern, how do private functions access the private properties of the module? I haven't seen any examples where developers do this. Is there any reason not to? var module = (function(){ // private property var…
Thomas
  • 5,736
  • 8
  • 44
  • 67
13
votes
2 answers

How do I professionally structure my module-pattern Javascript projects?

I've read about the Revealing Module Pattern and I love it. But what about large projects where the 'master-object' has tens of sub-objects and maybe hundreds of functions. I wouldn't want to be the one to place all that code in one anonymous…
Hubro
  • 56,214
  • 69
  • 228
  • 381
1
2 3
15 16