0

Having some problem extracting inner functions in javascript. Consider the following function:

this.zoomListener = d3.zoom()
    .on("zoom", (function(){
        new_xScale = d3.event.transform.rescaleX(this.xScale)
        new_yScale = d3.event.transform.rescaleY(this.yScale)
    }).bind(this));

To be a little more flexible i wanted to change the structur of the inner function like this:

this.zoomListener = d3.zoom()
        .on("zoom", zoomFunction);

function zoomFunction(){
            new_xScale = d3.event.transform.rescaleX(this.xScale)
            new_yScale = d3.event.transform.rescaleY(this.yScale)
        }

How can i bind the function now?

DerMann
  • 223
  • 1
  • 2
  • 13
  • @Rajesh: Not really a dupe, as the lexical context is quite different. OP obviously knew how to access the correct `this` as they used `bind`. – Amadan Nov 02 '18 at 09:47
  • @Amadan In my understanding, issue is that OP us sending a callback and is expecting it to have same context. So, in my understanding this was the appropriate dupe. Please share the appropriate one and I'll pull back my vote. – Rajesh Nov 02 '18 at 09:51

1 Answers1

-1
.on("zoom", zoomFunction.bind(this));

You can just use bind on the variable itself. bind just wants a function value, and it doesn't matter if you attach it to a function expression or to a variable that contains a function, it will do the same thing.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • Done sir. Also, please do not get offended. Apologies, if I was rude. Its just, I expect people like us to maintain discipline and lead by example. – Rajesh Nov 02 '18 at 09:47
  • I have a bit stricter definition of dupe than most, I suppose. That wasn't one. – Amadan Nov 02 '18 at 09:48
  • Sorry for asking a question that was already asked. Nevertheless thank you for the answer. – DerMann Nov 02 '18 at 09:51
  • 2
    @DerMann No need to apologies for asking a dupe. Its a normal thing. I just hope it helps you resolve your issue. – Rajesh Nov 02 '18 at 09:53