B
B
bqio2018-02-05 14:03:17
JavaScript
bqio, 2018-02-05 14:03:17

How to correctly format this with such a construction?

There is a code:

this.frames = [];
this.frame = 0;
        let l = this;
        this.canvas.onclick = function () {
            if (l.frame == l.frames.length - 1) return;
            l.frame++;
            l.render();
        }

Am I making a reference to the current object correctly? Or is it better to do it some other way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly Zharov, 2018-02-05
@bqio

1) You can use arrows () => {} instead of function() {}
2) You can use bind(this) to bind the context
3) You can store the context in a closure (as in your example)

A
Alexander Alekseev, 2018-02-05
@shure348

I will add to the previous answer that it is better to remake l = this into self = this this
is more generally accepted

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question