-1

I have a "class" Game definding a game and an inner "class" Snake defining a player in the game. My problem is that the member links in my Snake is showing as undefined whenever the move function is called and I can't figure out why this is.

Here's the Game definition (with a lot stripped out for readability). If needed, I can do a complete code dump.

function Game ( board, numBlocks )
{ 

    // ...

    this.speedMap = { "fast": 100, "medium": 300, "slow": 600 }; 
    this.curSpeed; 
    this.mover; 

    // ... 

    this.Snake = function ( game )
    {
        this.links; 
        this.dx;  
        this.dy; 

        this.createNew = function ( )
        {
            this.dx = 0; this.dy = 0;
            this.links = [];  

            // ...
        }

        this.move = function ( )
        {
            console.log(this.links); // test

            // ^ That is printing 'undefined'! Didn't I initialize it in 'createNew', though????

            // ... 

        }


    }

    this.startNew = function ( spd )
    {
        // ...        

        this.snake = new this.Snake(this);
        this.snake.createNew();

        // ... 

        this.curSpeed = spd;
        this.mover = setInterval(this.snake.move, this.speedMap[this.curSpeed]);
    }     

}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user5124106
  • 393
  • 2
  • 3
  • 11
  • `this.links;` --- what does this mean? – zerkms Jul 29 '15 at 04:47
  • 3
    `setInterval(this.snake.move.bind(this.snake), ...` – zerkms Jul 29 '15 at 04:51
  • @zerkms: That should be an answer if you couple it with a short explanation. – slebetman Jul 29 '15 at 04:54
  • Putting `this.someVariable;` alone in a statement doesn’t “declare” it or anything of the sort, so don’t do that. – Ry- Jul 29 '15 at 04:55
  • 1
    See this answer to understand how `this` really works: http://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628?s=1|3.8242#13441628 – slebetman Jul 29 '15 at 04:55
  • @slebetman yep, it was discussed million times here so I'd prefer to close it as a duplicate (haven't found something that matches exactly though) – zerkms Jul 29 '15 at 04:58

1 Answers1

0

In a function the this keyword always refers to the parent object. In your case it's probably the window object if your code runs in a browser.

Seb Bizeul
  • 968
  • 1
  • 10
  • 18
  • 1
    "to the parent object" --- where did you get the term "parent" from? – zerkms Jul 29 '15 at 05:00
  • probably a typo; should be `s/parent/current/` – Vidul Jul 29 '15 at 05:03
  • 1
    @CPH4 how about "global"? But even then the whole phrase would still be incorrect. – zerkms Jul 29 '15 at 05:03
  • Yes, global or head object if you prefer ! – Seb Bizeul Jul 29 '15 at 05:08
  • @zerkms why "global"? `this` can refer to any nested scope. If you mean this context and the `window` object, I agree that "global" is applicable. – Vidul Jul 29 '15 at 05:17
  • @CPH4 they used "object" term, not "scope". So you can say "global object", but I'm not sure what "current object" or "parent object" can mean. – zerkms Jul 29 '15 at 06:08
  • @zerkms feel free to interpret "current object" as the value bound to `this` in the current context. And not, the current scope change is not obligatory for `this` to change - you seem to mistakenly use the terms `scope` and `context` interchangeably. – Vidul Jul 29 '15 at 06:42
  • @CPH4 I do not use them interchangeably. And I did not say that scope has anything to do with `this` at all (not even close) – zerkms Jul 29 '15 at 07:10