无法读取未定义的属性停止-从类内部调用静态方法时 [英] Cannot read property stop of undefined - when calling a static method from inside class
本文介绍了无法读取未定义的属性停止-从类内部调用静态方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于某种原因,当我运行此代码时,即使不首先调用startRecording方法,也会在页面加载后立即出现此错误。
For some reason, when I run this code, even without first calling the startRecording method it gives me this error right after the page has loaded.
未捕获的TypeError:无法读取未定义的比例停止。
Uncaught TypeError: Cannot read proporty 'stop' of undefind.
我认为这与范围有关,但我不确定。 startRecording方法工作正常,但在stopRecording中发现问题。
I think it has something to do with scope but I'm not sure. The startRecording method works fine but it finds a problem in stopRecording.
RecorderClass
class RecorderClass
{
constructor() {
this.rec = ''
}
static startRecording() {
this.rec = new Recorder()
this.rec.record()
}
static stopRecording() {
this.rec.stop() // stop() of undefined
}
}
app.js
import RecorderClass from './RecorderClass.js'
recButton.addEventListener("click", RecorderClass.startRecording())
推荐答案
您应在类范围内声明Recorder引用,以便停止记录方法可以使用它。
You should declare Recorder reference in the scope of class, so that stop recording method can use it.
public class RecorderClass
{
static Recorder rec;
public RecorderClass()
{
}
public static void startRecording()
{
rec = new Recorder();
rec.record();
}
public static void stopRecording()
{
rec.stop();// stop() of undefined
}
}
这篇关于无法读取未定义的属性停止-从类内部调用静态方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文