Javascript scope addEventListener and this

后端 未结 3 2037
无人及你
无人及你 2020-12-07 23:22

I am a C# developer experimenting with JavaScript and I\'m trying to get my head around the scope :)

I have the following code which contains an addEventListe

3条回答
  •  无人及你
    2020-12-07 23:44

    How about

    function Keyboard() {
        this.keys = {};
        var self = this;
        this.handle_keydown = function(args) {
            self.keys[args.keyCode] = true;
        }
        this.listen = function() {
            window.addEventListener('keydown', this.handle_keydown);
        }
    }
    app.util.keyboard = new Keyboard();
    

提交回复
热议问题