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?

3
  • 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' Commented Sep 7, 2016 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. Commented Sep 7, 2016 at 22:18
  • 1
    you can use private npm as it accepts any git repository as dependency. package your code in small pieces is what modules are for (you can use commonjs, 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 ! Commented Sep 7, 2016 at 22:31

1 Answer 1

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;
}());
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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 immediately invoked function** eexpression, 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.
"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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.