BOOM! Thank you...that was exactly what I was looking for. Much easier on
the eyes and keeps everything away from Global
Thanks for everyone who responded so quickly.
On Monday, November 11, 2013 2:21:03 PM UTC-5, ajlopez wrote:
>
> Hmmm... maybe something like:
>
> function myfunction(...) {
> ...
> }
>
> function MyDice() {
> ...
> // calling my function
> myfuncion()
> }
>
>
> function MyDie() {
> ...
> // calling my function
> myfuncion()
> }
>
> module.exports = {
> MyDice: MyDice,
> MyDie: MyDie
> }
>
> I don't sure you need to export myfunction, but you can include it in the
> about module.exports = { ... }
>
> Angel "Java" Lopez
> @ajlopez
>
>
> On Mon, Nov 11, 2013 at 3:43 PM, Charles Angel
> <[email protected]<javascript:>
> > wrote:
>
>> I'm new to Node.js and finally making some headway before running into
>> module.exports
>>
>> I wanted to separate some code out into an external file, so I created
>> the file and called it probability.js
>>
>> The problem I ran into was having to define the function getRandomInt
>> twice to allow it to be called by the Die methods and by the primary server
>> code. I'm sure this is a scope issue, but I have not been able to figure
>> out how to resolve it in the proper way. Ideally, I want the getRandomInt
>> function defined one time within module.exports, but not part of Die or
>> Dice objects while still allowing the Die and Dice methods to call it. Any
>> assistance is appreciated.
>>
>>
>>
>> module.exports = {
>>
>> // Returns a random integer between min and max
>> // Using Math.round() will give you a non-uniform distribution!
>> getRandomInt: function(min, max) {
>> return Math.floor(Math.random() * (max - min + 1) + min);
>> },
>>
>> Die: function(numSides) {
>> // constructor;
>> var sides = numSides;
>> var lastRoll = -1;
>>
>> this.roll = function() {
>> console.log("sides: %d", sides);
>> lastRoll = getRandomInt(1, sides);
>> }
>>
>> this.getLastRoll = function() {
>> return lastRoll;
>> }
>> },
>>
>> Dice: function() {
>> var myDie = new Array();
>> var totalDie = 0;
>>
>> this.addDie = function(newDie) {
>> myDie.push(newDie);
>> totalDie = totalDie + 1;
>> }
>>
>> this.roll = function() {
>> for (i=0; i<totalDie; i++) {
>> myDie[i].roll();
>> }
>> }
>>
>> this.getTotalRoll = function() {
>> var total = 0;
>>
>> console.log("Get Total Roll");
>> for (i=0; i<totalDie; i++) {
>> console.log('Dice: %d = %d', i, myDie[i].getLastRoll());
>> total = total + myDie[i].getLastRoll();
>> }
>>
>> return total;
>> }
>> }
>> };
>>
>> // Returns a random integer between min and max
>> // Using Math.round() will give you a non-uniform distribution!
>> var getRandomInt = function(min, max) {
>> return Math.floor(Math.random() * (max - min + 1) + min);
>> }
>>
>>
>> // Returns a random number between 0 (inclusive) and 1 (exclusive)
>> var getRandom = function () {
>> return Math.random();
>> }
>>
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.