我正在尝试为加法问题生成器创建一个构造函数,但是当我运行代码时,我收到“ Uncaught ReferenceError :firstNum 未定义”
function GenAddProb()
{
this.firstNum = returnRandomInt(1,10);
this.secondNum = returnRandomInt(1,10);
this.ans = firstNum+secondNum;
}
我的构造函数的编写方式有问题吗?从我在教程中看到的情况来看,在构造函数中使用它们之前,我不必将 firstNum 和 secondaryNum 定义为变量。
最佳答案
this.ans = firstNum+secondNum;
正如它所说,firstNum 没有定义。您的意思是 this.ans = this.firstNum + this.secondNum
吗?
From what I've seen in tutorials I don't have to define firstNum and secondNum as variables before I use them in the constructor.
您不需要定义this
,如果这就是您的意思。在构造函数的上下文中,this
是您正在构造的对象。但如果您想创建和使用局部变量,则需要定义它们。
关于javascript - 当我尝试创建 Javascript 对象时未捕获引用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46996626/