Javascript that.init不是一个函数-Node.js
我对javascript和node.js几乎完全陌生,但我正在从事一个Connect 4机器人项目。为此,我正试图得到一个代码,我在互联网上找到的工作。这是我遇到的问题:Javascript that.init不是一个函数-Node.js,javascript,node.js,Javascript,Node.js,我对javascript和node.js几乎完全陌生,但我正在从事一个Connect 4机器人项目。为此,我正试图得到一个代码,我在互联网上找到的工作。这是我遇到的问题: Game(); function Game() { this.rows = 6; // Height this.columns = 7; // Width this.status = 0; // 0: running, 1: won, 2: lost, 3: tie this.depth = 4; // Search dep
Game();
function Game() {
this.rows = 6; // Height
this.columns = 7; // Width
this.status = 0; // 0: running, 1: won, 2: lost, 3: tie
this.depth = 4; // Search depth
this.score = 100000, // Win/loss score
this.round = 0; // 0: Human, 1: Computer
this.winning_array = []; // Winning (chips) array
this.iterations = 0; // Iteration count
var that = this;
that.init();
}
Game.prototype.init = function() {
// Generate 'real' board
// Create 2-dimensional array
var game_board = new Array(that.rows);
for (var i = 0; i < game_board.length; i++) {
game_board[i] = new Array(that.columns);
for (var j = 0; j < game_board[i].length; j++) {
game_board[i][j] = null;
}
}
Game();
函数游戏(){
this.rows=6;//高度
this.columns=7;//宽度
this.status=0;//0:跑步,1:赢,2:输,3:平局
this.depth=4;//搜索深度
this.score=100000,//赢/输分数
this.round=0;//0:人类,1:计算机
this.winning_数组=[];//winning(chips)数组
this.iterations=0;//迭代计数
var=这个;
init();
}
Game.prototype.init=函数(){
//生成“真实”板
//创建二维数组
var game_board=新数组(即.rows);
对于(变量i=0;i
当我尝试使用node.js运行它时,会出现以下错误:
that.init();
^
TypeError: that.init is not a function
at Game (C:\Users\*******\Desktop\New Folder\connect-four.js:21:10)
at Object.<anonymous> (C:\Users\*******\Desktop\New Folder\connect-four.js:7:1)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:390:7)
at startup (bootstrap_node.js:150:9)
that.init();
^
TypeError:that.init不是函数
在游戏中(C:\Users\******\Desktop\New Folder\connect four.js:21:10)
at对象。(C:\Users\******\Desktop\New Folder\connect four.js:7:1)
在模块处编译(Module.js:570:32)
在Object.Module.\u extensions..js(Module.js:579:10)
在Module.load(Module.js:487:32)
在tryModuleLoad时(module.js:446:12)
在Function.Module.\u加载(Module.js:438:3)
位于Module.runMain(Module.js:604:10)
运行时(bootstrap_node.js:390:7)
启动时(bootstrap_node.js:150:9)
到底是什么导致了这个错误?
提前感谢您的帮助!您得到了已提升和未提升的混合代码;
函数
声明被提升,但是在该函数的原型上定义init
的表达式没有,因为它是一个表达式。这意味着您的代码按以下顺序被调用
Game()
Game.prototype.init=function(){/*…*/}
Game.prototype.init=function(){/*…*/}
新游戏()
此外,如果你想让函数得到它自己的
这个(即如果它是一个构造函数),你需要以不同的方式调用它,例如使用new
你必须使用new
初始化一个对象,并且只有在init
被分配给Game.prototype.init
之后:
function Game() {
(...)
}
Game.prototype.init = function() { ... };
new Game();
在init()
函数中,必须使用this
(...)
var game_board = new Array(this.rows);
(...)
game_board[i] = new Array(this.columns);
为什么?
- 当您像代码那样调用
Game()
时,this
是全局对象(您可以通过设置global.init=…
进行验证,this.init()
调用然后在您的代码中工作)
- 添加到原型需要该原型的一个实例,即。
Game.prototype.init
仅继承给Game
newgame()
是创建具有
init
方法绑定到它,因此调用that.init()
实际上可以工作
- 函数内定义的变量仅在函数范围内可见(以及函数内定义的函数),即
仅在游戏
函数本身可见(也是游戏
操作符创建的游戏
对象的构造函数)
由于未在init
中定义,因此代码失败,但是此
有效,因为它引用的对象与构造函数中的对象相同(游戏
函数)
只需使用ES6类即可
class Game {
constructor(configuration) {
this.rows = configuration.rows
this.columns = configuration.columns
this.depth = configuration.depth
this.score = configuration.score
this.round = configuration.round
this.winning_array = configuration.winning_array
iterations = 0
}
init() {
// Generate 'real' board
// Create 2-dimensional array
var game_board = new Array(this.rows);
for (var i = 0; i < game_board.length; i++) {
game_board[i] = new Array(this.columns);
for (var j = 0; j < game_board[i].length; j++) {
game_board[i][j] = null;
}
}
}
}
// USAGE
const winning_array = []
const configuration = {
rows: 6,
columns: 7,
status: 0,
depth: 4,
score: 100000,
round: 0,
winning_array: winning_array,
iterations: 0,
}
const game = new Game(configuration)
game.init()
类游戏{
构造函数(配置){
this.rows=configuration.rows
this.columns=configuration.columns
this.depth=configuration.depth
this.score=configuration.score
this.round=configuration.round
this.winning\u array=configuration.winning\u array
迭代次数=0
}
init(){
//生成“真实”板
//创建二维数组
var game_board=新数组(this.rows);
对于(变量i=0;i
除了缺少新的
,永远不要调用超出其定义的构造函数。首先初始化原型方法,然后实例化。谢谢,这就解决了问题!将“that”更改为“this”是否意味着我可能引用了错误的对象,或者这没有什么区别?@Kaza123很高兴它有帮助,我添加了一个解释“…并且只有在定义了Game
之后”问题不是在调用Game
之前没有定义;而是Game.prototype.init
没有定义。并且“根据定义,这个是全局对象
不正确,或者至少有误导性。全局对象
是一个构造函数,而此
在OP的调用中没有引用它。“全局对象”,可以通过窗口
引用,这是设置为此
的内容。更清晰的测试应该是window.init=…
@斜视现在反映了您关于游戏.prototype.init
的观点。但是,我关于此
是全局对象的观点是正确的,请参阅。注意,问题与node.js有关,不是web浏览器。节点和浏览器都有一个全局对象。它只是通过一个不同的变量名引用自身。因此,与其