2017-05-13 33 views
0

我几乎全新的JavaScript和node.js,但我正在开发Connect 4机器人项目。为此,我试图让我在互联网上找到的代码正常工作。这是有问题的部分IM:that.init不是一个函数 - Node.js

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; 
    } 


} 

当我尝试用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)  

到底是什么造成这个错误? 在此先感谢您的帮助!

+1

除了缺少'new',* never *在其定义之上调用构造函数之外。首先初始化原型方法,然后实例化。 – Bergi

回答

1

你已经混合了悬挂和未悬挂的代码; function声明被吊起来,但是在该函数的原型上定义init的表达式不是,因为它是表达式。这意味着您的代码按以下顺序调用;

  1. 功能游戏定义
  2. Game()
  3. Game.prototype.init = function() { /* ... */ }

这意味着在步骤2的时候,当你调用你的函数,初始化尚未确定。

您需要更改代码的顺序,使其成为定义

  • Game.prototype.init = function() { /* ... */ }
  • new Game()
  • 此外,如果你想要的功能得到

    1. 功能游戏它自己的this(即如果它是一个构造函数),您需要以不同的方式调用它,例如与new

    +0

    谢谢,现在它工作! – Kaza123

    1

    你必须使用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()为您的代码做,thisby definition是全局对象
    • 增加了原型(可以通过设置global.init = ...this.init()通话,然后在你的代码工作验证)需要一个实例的原型,即 Game.prototype.init只继承到的Game
    • new Game()实例就是创建了一个具有 绑定了init方法的实例,因此呼吁that.init()将实际工作的函数中定义
    • 变量仅在该范围内可见功能(和在其内定义的函数),即that仅在Game函数本身可见(其也是由操作者new创建Game对象的构造函数)
    • that不定义然而代码失败,this的作品,因为它指的是同一个对象作为构造(该Game功能)
    +0

    谢谢,解决了它!可以将“那个”改为“这个”意味着我可能引用了错误的对象或者它没有什么区别? – Kaza123

    +0

    @ Kaza123很高兴帮助,我添加了一个解释 – miraculixx

    +0

    *“...并且只有在'Game'已被定义之后”*问题不在于'Game'在调用之前没有定义;它没有定义'Game.prototype.init'。而*“this'由定义是全局的”Object'“*是不正确的,或者至少是误导性的。全局的'Object'是一个构造函数,'this'并不在OP的调用中引用它。可以通过'window'引用的“全局对象”是设置为'this'的。一个更清晰的测试将是'window.init = ...' –

    -1

    只需使用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() 
    
    +0

    对不起,为什么downvote? – phra

    +1

    我没有低调,但我认为你得到了-1,因为你的帖子实际上并没有回答这个问题。 ES5的构造函数没问题,这是有效的JavaScript。 ES6类很棒,但请记住这只是句法糖......;) – Badacadabra