1

Looking for pure JavaScript answers please.

Using IIFE for a JavaScript game. Actually multiple games on multiple webpages. Suppose there is a common piece of code that needs to be used by all of these games. Say for example, a diceroller; 1d20, 3d6, etc.

What is the right way to do this? Should the IIFEs all be set to the global with unique names? I worry about setting to the global (perhaps I am too worried about that).

Does the diceroller need to be passed into the game IIFE? How to do this properly?

MauroPorras
  • 5,079
  • 5
  • 30
  • 41
FreddyNoNose
  • 478
  • 1
  • 7
  • 13
  • 1
    in 2016 I think you should use something like `npm` or `bower`, package your source into a small testable module and then declare your dependency in your main project. then you should use something like `browserify` or `webpack` to include your module inside your code, `var diceRoller = require('diceRoller');` or in ES6 `import diceRoller from 'diceRoller'` – johnnydev Sep 07 '16 at 22:15
  • 1
    I don't want to pay for private services on npm. I don't use bower. Also, isn't require part of node? I am not using node. Just just javascript and simple html file. – FreddyNoNose Sep 07 '16 at 22:18
  • 1
    you can use private npm as it accepts any [git repository as dependency](http://stackoverflow.com/questions/23210437/npm-install-private-github-repositories-by-dependency-in-package-json). package your code in small pieces is what modules are for (you can use [commonjs](http://requirejs.org/docs/commonjs.html), that is npm style with `require` via `require.js` or `browserify` ..., amd, or the new ES6 stuff with `import`) and you'll have module in your browser ! – johnnydev Sep 07 '16 at 22:31

1 Answers1

3

I think you want a Revealing Module Pattern, not an IIFE Pattern.

//Revealing Module Pattern (Public & Private) w Public Namespace 'game'
var game = (function() {

    // object to expose as public properties and methods such as game.roll
    var pub = {};

    //game.roll
    pub.roll = function () {
        //do your thing
        return randomIntFromInterval(1,6);
    };

    function randomIntFromInterval(min,max){
        return Math.floor(Math.random()*(max-min+1)+min);
    }

  //API
  return pub;
}());
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • Ok, so I should use a setter to pass in the diceRoller. The diceRoller isn't just a RNG it has helper functions. Your approach might be the way I should do this. Thanks. – FreddyNoNose Sep 07 '16 at 22:30
  • 1
    `Revealing Module Pattern, not an IIFE Pattern` this is wrong because that module you demostrate _is_ an IIFE. The only think an IIFE is is an **i**mmediately **i**nvoked **f**unction** **e**expression, so in general this `(function(){})()` is an IIFE. It's not something completely different to a revealing module (or any of the other permutations on the module pattern), in fact they all leverage it because an IIFE provides the level of isolation they require as a base and then they build on top of that. – VLAZ Sep 07 '16 at 22:40
  • 2
    "The [module] pattern is quite similar to an immediately-invoked functional expression (IIFE - see the section on namespacing patterns for more on this) except that an object is returned rather than a function." [Learning JavaScript Design Patterns by Addy Osmani](https://addyosmani.com/resources/essentialjsdesignpatterns/book/) – Ronnie Royston Sep 07 '16 at 23:17