Paul Trotter
Paul Trotter

Reputation: 607

'invoke a function as a method' that is within another 'function as a method'

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

Upvotes: 0

Views: 43

Answers (1)

Mitya
Mitya

Reputation: 34556

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.

Upvotes: 3

Related Questions