JavaScript 类型错误 : cannot read property of undefined

标签 javascript class typeerror

我正在尝试在 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/

相关文章:

class - 拆分 UML 类图?

ruby - 设置未初始化的实例变量

嵌入 Amazon 推荐 Javascript 小部件时出现 Javascript 错误

python 类型错误

python - unicodedata.normalize 在 python 中做什么?

javascript - 如何从数组值中删除逗号?

javascript - Kendo UI Angular Grid - 只需要显示一行内容

javascript - 转换打破父溢出

java - 匿名类怎么可以有参数呢?

javascript - 为什么 Node.js 中的异步函数不是异步的?