2

With the module below, how can I add functions to it which are not public? This isn't the revealing module. Suppose I don't want roll to be public? Not saying that is correct as roll is useful. But how to hide it?

/* global module */

module.exports = (function () {
  'use strict';
  var random;

  function Dice(random) {
    this.init(random);
  }

  Dice.prototype.init = function (random) {
    this.random = random || Math.random
  };

  Dice.prototype.d20 = function() {
    var max = 20;
    var min = 1;
    return this.roll(min, max);
  };

  Dice.prototype.d6 = function() {
    var max = 6;
    var min = 1;
    return this.roll(min, max);
  };

  Dice.prototype.roll = function(min, max) {        
    return Math.floor(this.random() * (max - min)) + min;
  };

  return Dice;
}());
Kir Chou
  • 2,980
  • 1
  • 36
  • 48
FreddyNoNose
  • 478
  • 1
  • 7
  • 13
  • 1
    Try this: `function roll(min, max) { return Math.floor(this.random() * (max - min)) + min; };` – styfle Aug 23 '17 at 00:40
  • 1
    This is *exactly* the revealing prototype pattern. Not sure why you think you cannot just declare a function in there? Have a look [here](https://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern/9321429#9321429) for example. – Bergi Aug 23 '17 at 02:07
  • 2
    You should omit the IIFE. In Commonjs modules, the whole module has its own scope. So just put everything directly in the file, without any wrapping. Then assign `module.exports = Dice` in the end. – Bergi Aug 23 '17 at 02:08

2 Answers2

2

Private functions are not really part of Javascript. But you can kind of emulate them.. Here is an example..

var Roller = (function () {
  
  //constructor
  function Roller(name) {
    this.name = name;
  }
  
  //public method
  Roller.prototype.callPrivate = function() {
    //just need to remember to pass this
    //to our private functions
    private_roll(this);
  }
  
  //private function
  function private_roll(that) {    
    console.log(that.name);
  }

  //lets return our class.
  return Roller;
})();

 
var roller = new Roller('Test Me!');
roller.callPrivate();

//these will fail, 
//private_roll(roller);
//roller.private_roll(roller);
//IOW: no acces to private_roll from outside
//the closure..
Keith
  • 22,005
  • 2
  • 27
  • 44
  • Thank you. As I said, I might not want to do it for the generic roll because who knows if someday I might not want to use it to act like d17! – FreddyNoNose Aug 24 '17 at 22:16
1

If you don't require roll to know about the dice you could just make in a normal function instead of a method on the class.

module.exports = (function () {
  function roll (min, max, random) {
    return Math.floor(random() * (max - min)) + min
  }

  function Dice (random) {
    this.init(random)
  }

  /**
   * rest of your class definition
   */

  Dice.prototype.d6 = function() {
    var max = 6
    var min = 1
    return roll(min, max, this.random)
  }
})())
3066d0
  • 1,853
  • 1
  • 14
  • 18