我遇到一个奇怪的问题,即访问对象的某些方法会返回" x不是函数"错误,但访问其他方法不会返回任何问题。 typeof查询确认它没有定义,但奇怪的是它只是在对象外部看起来未定义 - 对象内的调用解决没有问题。我将发布以下代码。
tile.js
class Tile{
constructor(x, y, z, b){
this.x = x;
this.y = y;
this.z = z;
this.board = b;
console.log(typeof(this.getBoard()));//returns "function"
this.bot = "NONE";
}
setBot(b){
this.bot = b;
}
getBoard(){
return this.board;
}
}
module.exports = Tile;
bot.js
class Bot {
constructor(t, p) {
this.tile = t;
console.log(typeof(this.tile.setBot));//returns "function"
console.log(typeof(this.tile.getBoard));//returns "undefined"
this.tile.setBot(this);
this.tile.getBoard().getBots().addBot(this);//throws not a function error
}
getTile(){
return this.tile;
}
}
module.exports = Bot;
编辑:从这里调用这些方法。
var Board = require('./tiles/board.js');
var Tires = require('./parts/core/tires.js');
var Bot = require('./bot.js');
class GameInstance{
constructor(){
this.board = new Board();
this.players = new Map();
var t = new Tires();
var b = new Bot(t, this.board.getTile(3,3));
}
}
module.exports = GameInstance;