如果addEventListener覆盖“ this”,那么如何访问此类内的“ this”属性?

时间:2019-10-05 12:42:05

标签: javascript class ecmascript-6

https://jsfiddle.net/hzkmL879/

class ExampleClass {

    constructor () {

        // document.addEventListener('touchstart', this.StartSomething)

        document.addEventListener('click', this.StartSomething)

    }

    StartSomething (e) {

        console.log('Starting something..')
        console.log(e)
        console.log(this)

        this.FinishSomething(e)

    }

    FinishSomething (e) {

        alert('Finishing something!')
        // e event is needed here for something..

    }

}

new ExampleClass()
  

未捕获的TypeError:this.FinishSomething不是函数       在HTMLDocument.StartSomething

0 个答案:

没有答案