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;
}());
3
  • 1
    Try this: function roll(min, max) { return Math.floor(this.random() * (max - min)) + min; }; Commented Aug 23, 2017 at 0: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 for example. Commented Aug 23, 2017 at 2: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. Commented Aug 23, 2017 at 2:08

2 Answers 2

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..

Sign up to request clarification or add additional context in comments.

1 Comment

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!
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)
  }
})())

2 Comments

I don't know why I couldn't do that. I looked at some other code today I did before where I did exactly what you said. My memory recall is getting bad. TY.
It happens to us all :)

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.