gpt4 book ai didi

Javascript:无法在原型(prototype)函数中调用函数

转载 作者:行者123 更新时间:2023-12-03 04:42:05 24 4
gpt4 key购买 nike

我遇到了一个愚蠢的 javascript 错误,我无法弄清楚。在下面的代码中,我定义了一个“CustomPlotV”函数。效果很好。然后我有原型(prototype)函数,但它们没有被调用(okF 没有显示)。我过去使用过类似的代码,所以这个概念应该可行。我一定是做了一些愚蠢的错误......

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

最佳答案

这是因为您正在调用稍后在代码中分配的方法。

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

tensionPlotValence.test(); // now call it here

关于Javascript:无法在原型(prototype)函数中调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43044247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com