S
S
sinevik2018-01-31 20:15:45
JavaScript
sinevik, 2018-01-31 20:15:45

Function context?

Tell me how to pass the context of our object to the getMouse function, so that it would see console.log(this.ourdiv.style);

getMouse(e){
           console.log(this.ourdiv.style);

        }


    MouseDown(e){
        var mousePosX = e.pageX;
                var mousePosY = e.pageY;
                console.log(this.ourdiv.style);
                var divPosX = this.ourdiv.offsetLeft;
                var divPosY = this.ourdiv.offsetTop;
                this.deltaX = mousePosX - divPosX;
                this.deltaY = mousePosY - divPosY;
                window.addEventListener("mousemove", this.getMouse);
                this.ourdiv.onmouseup = function() {
                    window.removeEventListener("mousemove", this.getMouse);
                }
  }

frYZD.png
All code
https://jsfiddle.net/qyeh4yL9/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim, 2018-01-31
@sinevik

In the constructor, you can "nail":
using the bind operator, your class method (getMouse) will always be called in the this context (which refers to your class at the time of the bind )

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question