【发布时间】:2017-06-22 19:05:50
【问题描述】:
文件 A.js
function Game() {
this.id = Math.random();
this.endTimeout = setTimeout(() => {
this.end(this);
return
}, this.duration * 60 * 1000);
}
Game.prototype.addPlayer = function(game, player, items) {
console.log('addPlayer to game.id ' + game.id)
if (game.totalItemAmount >= 50) {
clearTimeout(game.endTimeout)
game.end(game);
}
return
}
Game.prototype.end = function(game) {
game = new Game();
}
let game = new Game();
require('../../tests/B.js')(game)
文件 B.js
module.exports = function (game) {
let test = setInterval(() => {
for (var i = 0; i < 10; i++) {
game.addPlayer(game, {
playerID: '2134' + i,
nickname: "player" + i
},
}, 4000);
假设第一个随机 game.id 是 2389423942,addPlayer 方法将继续将玩家添加到 2389423942,即使在游戏结束之后,由于新游戏已经开始,id 现在是不同的了。
A.js 中game 的替换不应该也替换B.js 中的吗?如何修复这个错误?
【问题讨论】:
标签: javascript node.js modular-design