0

I have inherited some javascript code that I am struggling to work with. I believe I need to 'invoke a function as a method' that is within another 'function as a method'. I have included abridged code below.

  drawer = {
  ...
  ChartDrawer: function(var1,var2,etc)
{
 ...
 this.drawChart = function(node)
    {
    ... 
        $(this.jqValToggle + ' #valuation-box').click(function(){
            console.log('clicked');
            drawer.valToggle();
            var targetNode = addrFromRawAddr(rawAddr)
            showNarrative(targetNode);
            /// THIS IS WHERE I'D LIKE TO CALL drawChart FROM
        });
    }
}

Could you please suggest the syntax needed to call 'drawChart' from the location in the code specified above.

I would have thought I could use drawer.ChartDrawer.drawChart(node); but this gets 'Uncaught TypeError: drawer.ChartDrawer.drawChart is not a function'. I have also tried variations on using 'this.drawChart' with no success

Paul Trotter
  • 607
  • 7
  • 14
  • 1
    Almost a duplicate of [How to access the correct `this` inside a callback?](http://stackoverflow.com/questions/20279484/how-to-access-the-correct-this-inside-a-callback). It provides multiple solutions for accessing `this` inside a callback. – Felix Kling Dec 22 '16 at 16:17
  • Try `return arguments.callee(node);` after showNarrative(targetNode) call – Medet Tleukabiluly Dec 22 '16 at 16:26

1 Answers1

3

Try this:

    $(this.jqValToggle + ' #valuation-box').click(function(){
        /* ... */
        this.drawChart();
    }.bind(this));

By forcing the click handler to run in the current context, rather than in the context of the clicked element, we retain access to the drawChart method.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Mitya
  • 33,629
  • 9
  • 60
  • 107