I have a class below that controls a small HTML5 game. You will see a few lines in to the startGame method a object is saved to a property called keys, this is an instance of the Keys class.
After this, I call another method inside of this class called setEventHandlers. This should in theory then set handlers that will invoke callbacks which will send data to the keys object (stored as a property).
Whenever this.keys.keyDown or this.keys.keyUp are called it states that 'keys' is undefined.
I can't seem to find why this is happening, it looks like something to do with order of execution. Any ideas? Thanks in advance.
function Game() {
    this.startGame();
}
Game.prototype.startGame = function () {
    this.canvasElement = document.createElement("canvas");
    this.canvasContext = this.canvasElement.getContext("2d");
    document.body.appendChild(this.canvasElement);
    this.canvasElement.width = window.innerWidth;
    this.canvasElement.height = window.innerHeight;
    this.keys = new Keys();
    this.mouse = new Mouse();
    this.player = new Player(this.randomPosition().x, this.randomPosition().y);
    this.setEventHandlers();
};
Game.prototype.setEventHandlers = function () {
    var onKeyDown = function (e) {
        this.keys.keyDown(e.keyCode);
    },
        onKeyUp = function (e) {
            this.keys.keyUp(e.keyCode);
        },
        onMouseMove = function (e) {
            // this.mouse.move(e.x, e.y);
        },
        onMouseClick = function (e) {
            // this.mouse.click(e.x, e.y);
        },
        onWindowResize = function () {
            this.canvasElement.width = window.innerWidth;
            this.canvasElement.height = window.innerHeight;
        };
    // Keyboard events
    window.addEventListener("keydown", onKeyDown, false);
    window.addEventListener("keyup", onKeyUp, false);
    // Mouse events
    window.addEventListener("mousemove", onMouseMove, false);
    window.addEventListener("click", onMouseClick, false);
    // Window events
    window.addEventListener("resize", onWindowResize, false);
};
Game.prototype.randomPosition = function () {
    var randomX = Math.round(Math.random() * this.canvasElement.width),
        randomY = Math.round(Math.random() * this.canvasElement.height);
    return { x: randomX, y: randomY };
};