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
0
votes
1 answer

In Javascript/Coffeescript how can I mock out a module's public function in a test?

This is just like my previous question but this is about functions instead of variables. I've got a module and I want to mock out a function in it. Here's my coffeescript code: root = exports ? this root.prod = (-> iWantToBeMocked = -> alert…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0
votes
1 answer

In Javascript/Coffeescript how can I mock out a module's public variable in a test?

I just learned about the module pattern. I've written some code that's gotten sufficiently complex that I want to test a feature. The problem is, I can't figure out how to mock out a value. What follows is coffeescript, but I'll put the generated…
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
0
votes
1 answer

Module Pattern and scope

I'm trying to create encapsulated objects that can be inherited and that possess some public properties. Here's some code: var MyObject = (function() { function myObject() { //Public variables this.publicVar1 = 'hello'; …
adampetrie
  • 1,130
  • 1
  • 11
  • 23
0
votes
3 answers

Objects literal and 'this' in submodule pattern

Im working in a sub-module pattern code. Want to create sub-modules with objects literals, the problem is this for the objects inside the sub-module is MODULE and not my object literal. Any idea? var MODULE.sub = (function () { var myObject =…
Juan
  • 1
  • 2
0
votes
2 answers

Module/prototype and multiple instances

I'm trying to get a grip on "OOP" JavaScript techniques, and I began writing a small test application today. Basically, it's a game loop and on each update coordinates are to be increased so that an HTML element moves. The problem is I want to be…
Viktor
  • 487
  • 2
  • 8
  • 26
0
votes
2 answers

Grasping "OOP" JavaScript: module pattern with inheritance

I'm trying to grasp the module pattern with added inheritance. I come from a university background, with mostly Java in my trunk, but I have been working with web techniques for about ten years. I'm only about a year into JavaScript…
Viktor
  • 487
  • 2
  • 8
  • 26
0
votes
0 answers

Thumbnails Scroll value for custom slider

Got my hands dirty on custom image carousel. Wrote some jquery to make it work but stuck with one issue. Issue lies when "prev" & "next" links are clicked: when click "next", if last visible item is reached then thumbnails container DIV will…
Lokesh Yadav
  • 1,574
  • 5
  • 23
  • 50
0
votes
1 answer

first time implementing module pattern variable is undefined

here is my code var s; var AddEvent = { settings : { saveButton : $('#uploadfiles1'), cancelSpeech : $('.cancelSpeech'), datePicker : $(".datepicker"), eventName : $('input[name=eventname]'), …
Najeeb K
  • 147
  • 1
  • 8
0
votes
3 answers

Javascript Module Pattern and jquery Ajax wait issue

I have written a custom js module that basically sends messages and needs to wait for a response in order to continue: var manageBooking = (function (jQ) { //decalre private variables var domain, msgRecieved, msgResponse, validationValue; //decalre…
Scott Jones
  • 160
  • 3
  • 15
0
votes
2 answers

Multiple "classes" in same namespace, but in separate files

I have a namespace: Foo.Bar.Baz within which I have the Qux class. These were defined using the revealing module pattern: Foo.Bar.Baz = (function ($) { // the namespace/module function Qux() { // the Qux "class" is contained in this…
Bobby B
  • 2,287
  • 2
  • 24
  • 47
0
votes
1 answer

Javascript revealing module pattern Noob

I'm starting Addy Osmani's amazing book on javascript design patterns but can't seem to get off the ground. Can anyone tell me what's wrong here with my approach (I'm using Raphael, just for fun.): var myPaper = Raphael('container', '800',…
Ben
  • 11,082
  • 8
  • 33
  • 47
0
votes
2 answers

how to use window.setTimeout with javascript and the module pattern

i have this example: var myApp = (function() { var inputClick = function() { console.log('inputClick'); }; var loadRecentTimeout = function() { window.setTimeout("inputClick()",3000); }; return { …
Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
0
votes
1 answer

jQuery AJAX cross-module communication(module pattern)

I recently refactored my javascript/jquery application code to use the module pattern. I have two modules, lets say A and B. Module B contains a public method(say, Bmethod) that makes a jQuery AJAX JSONP call($.ajax) and passes the response in a…
gofeddy
  • 579
  • 8
  • 20
0
votes
3 answers

Why doing self execution when implement module pattern in javascript

I am just wondering why self execution code should be needed when implement module pattern in javascript. Following code is typical module pattern sample : var app = app || {}; app.model = app.model || {}; app.model.person = (function…
Ray
  • 4,038
  • 8
  • 36
  • 48
-1
votes
1 answer

Accessing a module array gives me a reference error

I am trying to understand the revealing-module pattern but when try to update or access the variables falling into errors and can anyone provide an example how to update/access the variables in revealing-module pattern I get main.js:20 Uncaught…
1 2 3
15
16