0

当被窗口全局事件调用时,自指针“this”不起作用。

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();

如何获得正确this的方法event_handler

4

0 回答 0