I have this function in Game.js
Game.prototype.onClick = function(event){
var x = event.x;
var y = event.y;
if (this.blueDeck[0].contains(x, y))
alert("Blue Deck Clicked");
}
OnClick gets called from this function in Main.js
canvas.addEventListener('mouseup', game.onClick, false);
and I have this function in Card.js
Card.prototype.contains = function(x, y){
return true;
}
The alert never comes up.
If I remove the if statement in onClick, the alert gets called.
Other functions like this.blueDeck[0].setDeckPos(w, h); work fine when called in Game.js.
Why is contains not returning true?
The update confirms my assumption.
thiswill refer to the DOM element. The value ofthisis determined on runtime, i.e. it depends on how the function is called, not where/how it was defined.Either use a closure:
or if you don’t need access to the element, you can use
.bind[MDN] (see this link for a polyfill for browser with no native support):Learn more about
this:this(general)this(emphasis on event handlers)