以下のモジュールで、公開されていない関数を追加するにはどうすればよいですか?これは明らかなモジュールではありません。ロールを公開したくないとします。ロールとしては正しいとは言えません。しかし、それを隠す方法は?JavaScriptモジュールプライベート関数
/* 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;
}());
この試すことができます: '機能ロール(MIN、MAX)を{*)(Math.floor(this.randomを返します(max-min))+ min; }; ' – styfle
これは明らかに見えるプロトタイプパターンです。どうしてあなたがそこに関数を宣言できないと思うのか分かりません。たとえば、[こちら](https://stackoverflow.com/questions/9248655/how-to-implement-inheritance-in-js-revealing-prototype-pattern/9321429#9321429)をご覧ください。 – Bergi
IIFEは省略してください。 Commonjsモジュールでは、モジュール全体が独自のスコープを持ちます。だから、ラッピングをせずにすべてを直接ファイルに入れてください。最後に 'module.exports = Dice'を割り当てます。 – Bergi