0

I use to ecma6. I have problem with access to method in class, when I use onclick. I would like to induce method, which can induce other method in class.

This works:

class Menu{
     nextIteration(){...}

     init(){
       var that = this;
       //...

       buttonStart.onclick = function () {
                if(!that._start) {
                //...
                    that.nextIteration();
                } else if(that._start && that._pause) {
                //..
                    that.nextIteration();
                }
            };
    }
}

Now I tried:

class Menu{
    nextIteration(){...}

    init(){
    //...
       buttonStart.onclick= clickStart;
    }

    //...
    clickStart(){
            var that = this;
            if(!that._start) {
    //...
                that.nextIteration();
            } else if(that._start && that._pause) {
    //...
                that.nextIteration();
            }
        }
  }

In my opinion event is not beautiful. I prefer induce method in class. I tried addEventListener and "this" refer to element DOM.

viko
  • 491
  • 6
  • 23
  • Thank you. Yes, it's works: `buttonStart.addEventListener('click', this.clickStart.bind(this));` – viko Aug 21 '16 at 11:50
  • Notice that `clickStart` doesn't work at all, it needs to be `this.clickStart` at least. And even that doesn't work, for the reasons outlined in the duplicate. – Bergi Aug 21 '16 at 13:37

0 Answers0