我必须首先声明我要修改Game Dev Tycoon,但是游戏的源代码是事先运行的,因此在运行此代码之前将“ Game”对象放置在全局范围内。

new Game({
  conferenceHype: 0
}).gameSize; /* proves that it exists */
(function() {
  var Game, oldGame, oldGameConst;
  oldGameConst = Game;
  oldGame = oldGameConst.prototype; /* fails here because it thinks oldGameConst is undefined */
  Game = function(company) {
    oldGameConst.call(this, company);
    this.company = company;
  };
  Game.prototype = oldGame;
})();


有人对为什么会失败有任何想法吗?

最佳答案

  var Game, oldGame, oldGameConst;
  oldGameConst = Game;
  oldGame = oldGameConst.prototype; /* fails here because it thinks oldGameConst is undefined */



那么你:


创建一些局部变量,所有局部变量均以undefined开头
Game的值(因为您刚刚声明,所以将其复制到undefined
出于上述原因,请尝试阅读oldGameConst…,它是oldGameConst.prototype


如果要从更广泛的范围访问undefined:请勿使用Game对其进行屏蔽。

09-25 19:27