Javascript Using Bind Within An Object, How Can I Access Object This?
I am building an event manager for a little game that I am creating and have stumbled on a little problem (I don't know if it is a design pattern problem or if there is a solution
Solution 1:
You will need to use a closure if you can't bind it. And I wouldn't bind the mousedown
function to game
either, as it's not a method on it. Simplicity rules:
o.Events = function Events(game) {
"use strict";
this.mousedown = false;
var that = this;
game.element.addEventListener("mousedown", function mousedown(e) {
/* use
e - the mouse event
this - the DOM element ( === e.currentTarget)
that - the Events instance
game - the Game instance (or whatever was passed)
*/
that.mousedown = true;
// For each layer
game.layers.forEach(function(layer) {
// Layer is listening
if (layer.listening && layer.mouse.x && layer.mouse.y)
console.log("mousedown");
});
}, false);
};
Post a Comment for "Javascript Using Bind Within An Object, How Can I Access Object This?"