I am new to NodeJS development, and I've set up Visual Studio Code to develop JavaScript for node applications.
I was following a lecture on Pluralsight, and we had just made it to the section about Objects. What the teacher did was make two scripts, dice.js
and program.js
and put them in the same folder. I did the same.
They look like this:
dice.js:
var die = {
size: 6,
totalRolls: 0,
roll: function() {
this.totalRolls++;
return Math.ceil(this.size * Math.random());
}
};
exports.game = die;
program.js
var dice = require("./dice");
var die = dice.die;
die.size = 10;
console.log(die.roll());
console.log(die.roll());
console.log(die.roll());
console.log("Total rolls " + die.totalRolls);
console.log(die);
Whenever I try and run the program.js file through Visual Studio Code however, it tells me that the property "size" is undefined. If I try and use the dot connector I get this:
So the size isn't there. The instructors program worked just fine, but me doing this keeps giving me the error. I've looked everywhere to try and understand what could have gone wrong. My guess is that I don't use require()
correctly, or something. Coming from Strongly Typed compiled languages, this is very confusing to me.
What did I miss?
You exporting a thing called game but importing die, so naturally this wont work.
// dice.js
exports.game = die;
// program.js
var dice = require("./dice");
var die = dice.die; // <- this is wrong, change to .game:
var die = dice.game;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments