0

Hello I am trying to create Tetris in JavaScript. I have map object with a variable called data. When I'm calling a function from the object, the 'this' is no longer the object but the local function I think. I have tried using declaring a variable _this = this in the objects constructor but doesn't help.

function Map(width, height){
  var _this = this;
  this.data = [];
  for(var i = 0; i < width; i++){
    var row = [];
    for(var n = 0; n < height; n++){
      row.push("0");
    }
    this.data.push(row);
  }
}

Map.prototype.add = function(x, y, shape){
  for(var i in shape){
    for(var n in shape[i]){
      _this.data[x+i][y+n] = shape[i][n];
    }
  }
}

var colMap = new Map(23,30);
var selectedShape = new FallingShape(0, 0, getShapeArray(randomShapeId(), 0));
colMap.add(selectedShape.x, selectedShape.y, selectedShape.shapeArray);

I'm trying to change the values of data but can't because of the scope, I've tried the '_this' trick but doesn't work. Can anyone help me understand?

Heres a link to a codepen page if you want to see the whole code. http://codepen.io/taylorlutjen/pen/OPGwoo?editors=001

3
  • Initialize _this before you run the function and you should be set. I can't tell if this is working with your Codepen example though, so I'll keep this as a comment. Commented Apr 3, 2015 at 3:38
  • I'm pretty sure it is being initialized, because how would I run the function? Commented Apr 3, 2015 at 3:39
  • Well, the way the code is now, you're creating a local variable called _this that belongs to function Map - it won't be accessible anywhere else outside of that function. Pointy explained it better than I did though. Commented Apr 3, 2015 at 3:41

1 Answer 1

2

Your _this local variable created in your constructor function is local to that function — it won't be available in the other function.

The good news is that you won't really need it - just use this in that .add() function.

Local variables in constructor functions are no different than local variables in any other function. They're not magic, and they definitely won't be available to other functions added to the constructor's prototype. The reason for creating a _this or that or self copy of the value of this is to be able to create things like callback functions that need a reference to the outer context object. None of your code (posted here) needs that.

Sign up to request clarification or add additional context in comments.

6 Comments

If I use just this.data, it still gives me an error because it can't find 'this'
@electrope: this always exists. The question is which value it has. And in your example there is nothing that indicates this would be anything else than the Map instance.
@electrope well if that's the case then it boils down to how exactly that .add() function is being called. You didn't post that code, but there are common things to do that result in functions like that becoming "detached" from any object instances.
Well, I think the .add() function has it, so I guess I'm asking how can I get to the object's variables?
@electrope again, it depends on how .add() is called - prototype methods in JavaScript have no intrinsic relationship with object instances. It all depends on how the methods are actually called.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.