文件 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中的吗?如何修复错误?