mardi 8 novembre 2016

Reveal module pattern: Where to insert variables for module access

I'm learning reveal module pattern, and I'm trying to create a reusable function. (In my project, the function will make the page scroll. I don't think it's necessary to post the whole code here. I'm just putting the concept.)

The basic overview is, there's a function that will not return anything. There's no need for public variables. Here's the code. The questions are at the comments in the code:

JSFiddle

var MyModule = (function() {
  // Is this the correct place to insert the
  // variables that will be used throughout 'MyModule'?
  var foo = 'foo',
    foo2 = 'foo2',
    param1 = null;

  var MyModule = function(_param1) {
    param1 = _param1;

    logParam();
  };

  function init() {
    foo = 'something';
  }

  function logParam() {
    console.log(param1);
  }

  init();

  return MyModule;
})();

var module = new MyModule('Some Paramater');

// Is this okay? Does it still follow reveal module pattern?
MyModule('Some Other Paramater');
// Or do I always have to do like this:
var module = new MyModule('Some Paramater');

Aucun commentaire:

Enregistrer un commentaire