0

to I ran into a problem with the context of a function that is passed as an argument.

The following Code shows the initial situation.

...

        this._handleListener(cb.fromObj.addEventListener);
},

_handleListener: function(func) {
        func('vr-resize', this.ConnectionBehavior.resize);
        func('vr-delete', this.ConnectionBehavior.delete);
},

...

The code snippet didn't work as expected. After changing the second line and bind the context from cb.fromObj to the function it worked.

Changed line looks like this:

this._handleListener(cb.fromObj.addEventListener.bind(cb.fromObj));

Now are my question: Why do I need bind? Which context does the function use without bind?

Thanks in advance!

magoeke
  • 111
  • 6

0 Answers0