0

I'm using forEach inside es6 class and bind a function


My question is: how to access this in another scope of es6 functions ||

and difference between call function without bind or with this.logElement or this.logElement.bind(this)


What do I want to do: get element clicked with this not whole class

class Script {
    constructor(element) {
        super(element)
        this.ref = {
            el: []
        }
        this.ref.el.forEach((item, index) => {
            item.addEventListener('click', this.logElement.bind(this));
        })
    }

    logElement() {
        const that = this;
        console.log(that); // log element clicked not whole class
    }
}

export default Script

0 Answers0