0

The self pointer 'this' doesn't work when being called by window global event.

class foo {
    bar(){
        console.log("bar is called");
    }
    
    event_handler(){
        // At the time being called, 'this' here is 'window'
        // instead of instance of foo
        this.bar();
    } 

    init(){
        window.onresize = this.event_handler;
    }
}

var inst = new foo();
inst.init();

How to get the correct this in the method event_handler?

Dee
  • 7,455
  • 6
  • 36
  • 70

0 Answers0