Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript可以';t在另一个内部调用原型函数_Javascript_Prototype - Fatal编程技术网

JavaScript可以';t在另一个内部调用原型函数

JavaScript可以';t在另一个内部调用原型函数,javascript,prototype,Javascript,Prototype,我一直在寻找答案,但我发现的一切似乎都表明我没有做错任何事。我试图在另一个函数中调用一个原型函数,但我一直得到一个错误,即它不是一个函数 出于某种原因,它只发生在一个特定的方法(勾选)中,其余的方法与相同的方法没有问题 这是我的密码: Timer.prototype.getTimeRemaining = function(){ return this.startTime - this.currentTimeTimed; } Timer.prototype.updateElement = f

我一直在寻找答案,但我发现的一切似乎都表明我没有做错任何事。我试图在另一个函数中调用一个原型函数,但我一直得到一个错误,即它不是一个函数

出于某种原因,它只发生在一个特定的方法(勾选)中,其余的方法与相同的方法没有问题

这是我的密码:

Timer.prototype.getTimeRemaining = function(){
  return this.startTime - this.currentTimeTimed;
}

Timer.prototype.updateElement = function(){
  document.getElementById("clock").innerHTML = this.getTimeRemaining();
  return true;
}

Timer.prototype.tick = function(){
  if (this.running){
    this.currentTimeTimed += timePassedInMilliseconds;
  }
  this.getTimeRemaining();
  this.updateElement();
}
因此,我得到的错误出现在tick()中:uncaughttypeerror:this.getTimeRemaining()不是一个函数

在进行更多测试时,我想我找到了原因:我想这是因为tick函数是从一个间隔调用的。我不确定,但这是我能想到的唯一不同于其他函数的东西。但是我不知道我应该如何改变我的代码,如果它是由间隔引起的


提前谢谢。

我发现我需要在我的设置间隔中使用.bind(这个)。

也许你有一些bindind问题。。。请告诉我们您的
setInterval
如何。在
this.getTimeRemaining()
行中的
勾选
。当它停在那里时,检查
this
的值。从那里开始。您可以考虑使用呼叫或应用: