我正在尝试在 Javascript 中创建一个堆栈类,但我不断收到上述错误的变体。我知道是构造函数的问题,但就是找不到问题出在哪里。这是我的类(class):
"use strict"
class Stack {
constuctor() {
this.items = [];
}
emptyArray() {
if (this.items === 0) {
this.error = "The stack is empty";
}
return this.error;
}
isEmpty() {
return this.items === 0;
}
push(x) {
return this.items.push(x);
}
pop() {
if (this.items >= 1) {
return this.items.pop();
}
else {
throw new emptyArray();
}
}
size() {
return this.items.length;
}
peak() {
let el1 = this.items.pop();
let el2 = this.items.pop();
this.items.push(el2);
this.items.push(el1);
return el2;
}
}
new Stack().size();
最佳答案
构造函数
拼写错误:
constuctor() {
this.items = [];
}
关于JavaScript 类型错误 : cannot read property of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42169424/