1

Somehow when executing this code, I get the alert from line 29 .mouseOnSeat. But I don't know why this.seats is null, while in the draw function it is not. I call the init function from html5.

//init called by html5
function init() {
  var cinema = new Cinema(8, 10);
  cinema.draw("simpleCanvas");

  var canvas = document.getElementById("simpleCanvas");
  //add event listener and call mouseOnSeat
  canvas.addEventListener('mousedown', cinema.mouseOnSeat, false);
}

var Cinema = (function () {
  function Cinema(rows, seatsPerRow) {
    this.seats = [];
    this.rows = rows;
    this.seatsPerRow = seatsPerRow;

    var seatSize = 20;
    var seatSpacing = 3;
    var rowSpacing = 5;

    var i;
    var j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < seatsPerRow; j++) {
            this.seats[(i * seatsPerRow) + j] = new Seat(i, j, new Rect(j * (seatSize +   seatSpacing), i * (seatSize + rowSpacing), seatSize, seatSize));
        }
    }
  }

  Cinema.prototype.mouseOnSeat = function (event) {
    //somehow this is null
    if (this.seats == null) {
        alert("seats was null");
        return;
    }
    for (var i = 0; i < this.seats.length; i++) {
        var s = this.seats[i];
        if (s.mouseOnSeat(event)) {
            alert("Mouse on a seat");
        }
    }
    alert("Mouse not on any seat");
  };

  Cinema.prototype.draw = function (canvasId) {
    var canvas = document.getElementById(canvasId);
    var context = canvas.getContext('2d');
    var i;
    //somehow this isn't
    for (i = 0; i < this.seats.length; i++) {
        var s = this.seats[i];
        context.beginPath();
        var rect = context.rect(s.rect.x, s.rect.y, s.rect.width, s.rect.height);
        context.fillStyle = 'green';
        context.fill();
    }
  };
  return Cinema;
})();

I tried a lot, like creating a self variable (var self = this ) and then calling from self.mouseOnSeat, it was suggested on another post, but I didn't figure it out.

4
  • What is line 29? Could you provide a jsfiddle? Commented Nov 2, 2014 at 20:52
  • Are you sure that the two definitions of Cinema are not conflicting? Commented Nov 2, 2014 at 21:10
  • jsfiddle.net/2eu7841h, it is now in line 32 Commented Nov 2, 2014 at 21:24
  • Creating the self variable only works if your function definition is inside the function that has the correct this. For example if you did not have a separate mouseOnSeat function but defined that function inline inside the addEventListener call, you could use that trick to replace this with self and there would be no problems. Commented Nov 2, 2014 at 21:28

2 Answers 2

1

The problem is that when you call addEventListener, the variable this does not carry along to the function call. This means that this is not your object.

You workaround is sound, you can use it. Or alteratively change your addEventListener call to:

canvas.addEventListener('mousedown', cinema.mouseOnSeat.bind(this), false);

Do note that you might need to use a polyfill to get Function.prototype.bind for older browsers, although it is very well supported currently. See caniuse.

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

Comments

0

I found a workaround : canvas.addEventListener('mousedown', function (event) { cinema.mouseOnSeat(event); }, false);

But I have no clue why

Comments

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.