0

I am having a silly javascript bug that I can't figure out. In the code below, I define a 'CustomPlotV' function. That works fine. Then I have prototype functions on that, but they aren't called (okF is not displayed). I have used similar code in the past, so this concept should work. I must be doing something silly wrong...

Plunker code.

        $('#log').text("oka");  //printed fine
        tensionPlotValence.test();

        $('#log').text("okb");  //not printed



        CustomPlotV.prototype.test = function test() {
            $('#log').text("okF"); //not printed

        }

        function CustomPlotV(jElement, data, options) {

            this.plot = $.plot(jElement, data, options);
            this.options = options;
            this.place = jElement;
            this.direction = 1;
            this.crossHairPos = this.plot.getAxes().xaxis.min;
        }
dorien
  • 5,265
  • 10
  • 57
  • 116
  • You are using code in wrong order. It should be. `CustomPlotV ` then `CustomPlotV.prototype.test` then `tensionPlotValence.test();` – Ruhul Amin Mar 27 '17 at 10:49
  • I tried switching the order, but no effect (see Plunker) – dorien Mar 27 '17 at 10:51
  • Check the console: `"jQuery.Deferred exception: tensionPlotValence is not defined ReferenceError: tensionPlotValence is not defined"` - You're defining `tensionPlotValence` in the success handler of a `$.get()` call – Andreas Mar 27 '17 at 10:53
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Andreas Mar 27 '17 at 10:54
  • Ah, I missed the });. Thanks! – dorien Mar 27 '17 at 11:00
  • Why did you accept the Answer of Jai? That's clearly not the solution... – Andreas Mar 27 '17 at 11:13
  • After I fixed what you suggested I also needed to change the order. If you write the full answer I'm happy to accept that – dorien Mar 27 '17 at 11:33
  • I won't add an answer on a question I've voted to close :) – Andreas Mar 27 '17 at 11:57

1 Answers1

1

It is because you are calling a method which is assigned later in the code.

CustomPlotV.prototype.test = function test() {
    $('#log').text("okF"); //not printed
}

tensionPlotValence.test(); // now call it here
Jai
  • 74,255
  • 12
  • 74
  • 103
  • I had tried that, but doesn't seem to be working: http://plnkr.co/edit/OVk3ZDpqHXLn2qf5f3TE?p=preview – dorien Mar 27 '17 at 10:50