I am making a game using Javascript. I use webpack to build my modules, so at the end of every Javascript file I use module.exports. Here is an example:
var Spaceship = function(options) {
this.position = options.position
this.name = options.name
}
module.exports = Spaceship
var Spaceship = require("./spaceship");
var Game = function() {
this.num_spaceships = 5;
this.spaceships = [];
this.add_spaceships();
}
Game.prototype.add_spaceships = function() {
for(var i = 0; i < this.num_spaceships; i++) {
this.spaceships.push(this.randomSpaceship
}
}
Game.prototype.randomSpaceship = function() {
}
At each of the debug points above, if I open the Chrome dev tools and type Spaceship, I getUncaught ReferenceError: Spaceship is not defined(…)
If I change the function randomSpaceshipas follows:
Game.prototype.randomSpaceship = function() {
var s = new Spaceship();
}
Then in DEBUGGER 3 it is now defined Spaceship(if I open the dev tools, I get that Spaceship is a function).
Why is this happening? I suggested that this could be due to a variable lift, perhaps, but I declare and assign a Spaceship variable at the top of the game.js.