Search code examples
javascriptflot

Javascript: can't call function in protoype function


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;
        }

Solution

  • 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